|
| 1 | +import argparse |
| 2 | + |
| 3 | +from jenkins_cli.cli import get_jobs_legend |
| 4 | +from jenkins_cli.version import version |
| 5 | + |
| 6 | + |
| 7 | +def load_parser(): |
| 8 | + """ |
| 9 | + Create a parser and load it with CLI arguments |
| 10 | +
|
| 11 | + Returns: ArgumentParser instance |
| 12 | + """ |
| 13 | + parser = argparse.ArgumentParser(prog='jenkins', |
| 14 | + description='Host, username and password may be specified either by the command line arguments ' |
| 15 | + 'or in the configuration file (.jenkins-cli). Command line arguments have the highest priority, ' |
| 16 | + 'after that the .jenkins-cli file from current folder is used. If there is no' |
| 17 | + '.jenkins-cli file in the current folder, settings will be read from .jenkins-cli located in the home' |
| 18 | + 'folder') |
| 19 | + parser.add_argument('--host', metavar='jenkins-url', help='Jenkins Host', default=None) |
| 20 | + parser.add_argument('--username', metavar='username', help='Jenkins Username', default=None) |
| 21 | + parser.add_argument('--password', metavar='password', help='Jenkins Password', default=None) |
| 22 | + parser.add_argument('--version', '-v', action='version', version='jenkins-cli %s' % version) |
| 23 | + |
| 24 | + subparsers = parser.add_subparsers(title='Available commands', dest='jenkins_command') |
| 25 | + |
| 26 | + jobs_parser = subparsers.add_parser('jobs', |
| 27 | + help='Show all jobs and their statuses', |
| 28 | + formatter_class=argparse.RawTextHelpFormatter, |
| 29 | + description="Status description:\n\n" + "\n".join(get_jobs_legend())) |
| 30 | + jobs_parser.add_argument('-a', help='show only active jobs', default=False, action='store_true') |
| 31 | + jobs_parser.add_argument('-p', help='show only jobs in build progress', default=False, action='store_true') |
| 32 | + |
| 33 | + subparsers.add_parser('queue', help='Show builds queue') |
| 34 | + |
| 35 | + subparsers.add_parser('building', help='Build executor status') |
| 36 | + |
| 37 | + builds_parser = subparsers.add_parser('builds', help='Show builds for the job') |
| 38 | + builds_parser.add_argument('job_name', help='Job name of the builds') |
| 39 | + |
| 40 | + start_parser = subparsers.add_parser('start', help='Start job') |
| 41 | + start_parser.add_argument('job_name', help='Job to start', nargs='*') |
| 42 | + |
| 43 | + start_parser = subparsers.add_parser('info', help='Job info') |
| 44 | + start_parser.add_argument('job_name', help='Job to get info for') |
| 45 | + |
| 46 | + set_branch = subparsers.add_parser('setbranch', help='Set VCS branch (Mercurial or Git)') |
| 47 | + set_branch.add_argument('job_name', help='Job to set branch for') |
| 48 | + set_branch.add_argument('branch_name', help='Name of the VCS branch') |
| 49 | + |
| 50 | + stop_parser = subparsers.add_parser('stop', help='Stop job') |
| 51 | + stop_parser.add_argument('job_name', help='Job to stop') |
| 52 | + |
| 53 | + console_parser = subparsers.add_parser('console', help='Show console for the build') |
| 54 | + console_parser.add_argument('job_name', help='Job to show console for') |
| 55 | + console_parser.add_argument('-b', '--build', help='job build number to show console for (if omitted, last build number is used)', default='') |
| 56 | + console_parser.add_argument('-n', help='show first n lines only(if n is negative, show last n lines)', type=int) |
| 57 | + console_parser.add_argument('-i', help='interactive console', default=False, action='store_true') |
| 58 | + console_parser.add_argument('-t', '--interval', help='refresh interval in seconds (in case of interactive console -i)', default=3, type=check_nonnegative) |
| 59 | + |
| 60 | + console_parser = subparsers.add_parser('changes', help="Show build's changes") |
| 61 | + console_parser.add_argument('job_name', help='Job to show changes for') |
| 62 | + console_parser.add_argument('-b', '--build', help='job build number to show changes for (if omitted, last build number is used)', default='') |
| 63 | + |
| 64 | + return parser |
| 65 | + |
| 66 | + |
| 67 | +def check_nonnegative(value): |
| 68 | + """ |
| 69 | + Checks if (possibly string) value is non-negative integer and returns it. |
| 70 | +
|
| 71 | + Raise: |
| 72 | + ArgumentTypeError: if value is not a non-negative integer |
| 73 | + """ |
| 74 | + try: |
| 75 | + ivalue = int(value) |
| 76 | + if ivalue < 0: |
| 77 | + raise ValueError() |
| 78 | + except: |
| 79 | + raise argparse.ArgumentTypeError("Value must be a non-negative integer: %s" % value) |
| 80 | + return ivalue |
0 commit comments