Skip to content

Commit 91154ad

Browse files
committed
Move all argparsing logic to separate file
1 parent 09d6e39 commit 91154ad

3 files changed

Lines changed: 84 additions & 71 deletions

File tree

jenkins_cli/__init__.py

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,15 @@
11
from __future__ import print_function
22

3-
import argparse
43
from jenkins import JenkinsException
54

6-
from jenkins_cli.cli import JenkinsCli, CliException, get_jobs_legend, check_nonnegative
7-
from jenkins_cli.version import version
5+
from jenkins_cli.cli import JenkinsCli, CliException
6+
from jenkins_cli.cli_arguments import load_parser
87

98

109
def main():
11-
parser = argparse.ArgumentParser(prog='jenkins',
12-
description='Host, username and password may be specified either by the command line arguments '
13-
'or in the configuration file (.jenkins-cli). Command line arguments have the highest priority, '
14-
'after that the .jenkins-cli file from current folder is used. If there is no'
15-
'.jenkins-cli file in the current folder, settings will be read from .jenkins-cli located in the home'
16-
'folder')
17-
parser.add_argument('--host', metavar='jenkins-url', help='Jenkins Host', default=None)
18-
parser.add_argument('--username', metavar='username', help='Jenkins Username', default=None)
19-
parser.add_argument('--password', metavar='password', help='Jenkins Password', default=None)
20-
parser.add_argument('--version', '-v', action='version', version='jenkins-cli %s' % version)
21-
22-
subparsers = parser.add_subparsers(title='Available commands', dest='jenkins_command')
23-
24-
jobs_parser = subparsers.add_parser('jobs',
25-
help='Show all jobs and their statuses',
26-
formatter_class=argparse.RawTextHelpFormatter,
27-
description="Status description:\n\n" + "\n".join(get_jobs_legend()))
28-
jobs_parser.add_argument('-a', help='show only active jobs', default=False, action='store_true')
29-
jobs_parser.add_argument('-p', help='show only jobs in build progress', default=False, action='store_true')
30-
31-
subparsers.add_parser('queue', help='Show builds queue')
32-
33-
subparsers.add_parser('building', help='Build executor status')
34-
35-
builds_parser = subparsers.add_parser('builds', help='Show builds for the job')
36-
builds_parser.add_argument('job_name', help='Job name of the builds')
37-
38-
start_parser = subparsers.add_parser('start', help='Start job')
39-
start_parser.add_argument('job_name', help='Job to start', nargs='*')
40-
41-
start_parser = subparsers.add_parser('info', help='Job info')
42-
start_parser.add_argument('job_name', help='Job to get info for')
43-
44-
set_branch = subparsers.add_parser('setbranch', help='Set VCS branch (Mercurial or Git)')
45-
set_branch.add_argument('job_name', help='Job to set branch for')
46-
set_branch.add_argument('branch_name', help='Name of the VCS branch')
47-
48-
stop_parser = subparsers.add_parser('stop', help='Stop job')
49-
stop_parser.add_argument('job_name', help='Job to stop')
50-
51-
console_parser = subparsers.add_parser('console', help='Show console for the build')
52-
console_parser.add_argument('job_name', help='Job to show console for')
53-
console_parser.add_argument('-b', '--build', help='job build number to show console for (if omitted, last build number is used)', default='')
54-
console_parser.add_argument('-n', help='show first n lines only(if n is negative, show last n lines)', type=int)
55-
console_parser.add_argument('-i', help='interactive console', default=False, action='store_true')
56-
console_parser.add_argument('-t', '--interval', help='refresh interval in seconds (in case of interactive console -i)', default=3, type=check_nonnegative)
57-
58-
console_parser = subparsers.add_parser('changes', help="Show build's changes")
59-
console_parser.add_argument('job_name', help='Job to show changes for')
60-
console_parser.add_argument('-b', '--build', help='job build number to show changes for (if omitted, last build number is used)', default='')
61-
10+
parser = load_parser()
6211
args = parser.parse_args()
12+
6313
try:
6414
if args.jenkins_command is None:
6515
parser.print_help()

jenkins_cli/cli.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import sys
44
import datetime
55
from time import time, sleep
6-
import argparse
76
import jenkins
87
import socket
98
from xml.etree import ElementTree
@@ -72,22 +71,6 @@ def xml_to_string(root):
7271
return ElementTree.tostring(root, encoding=('unicode' if sys.version_info[0] == 3 else None))
7372

7473

75-
def check_nonnegative(value):
76-
"""
77-
Checks if (possibly string) value is non-negative integer and returns it.
78-
79-
Raise:
80-
ArgumentTypeError: if value is not a non-negative integer
81-
"""
82-
try:
83-
ivalue = int(value)
84-
if ivalue < 0:
85-
raise ValueError()
86-
except:
87-
raise argparse.ArgumentTypeError("Value must be a non-negative integer: %s" % value)
88-
return ivalue
89-
90-
9174
class CliException(Exception):
9275
pass
9376

jenkins_cli/cli_arguments.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)