Command line to execute python script

We get jenkins job number here Jenkins

Use argparse to implement call python script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
parser = argparse.ArgumentParser(description='Manage Jenkins Job')
parser.add_argument('-v', '--version', action='store_true', help='Show version')
parser.add_argument('-V', '--view', dest='view_name', help='Get Jenkins job last build number by view')


args = parser.parse_args()

if len(sys.argv) == 1:
parser.print_help()
elif len(sys.argv) == 2:
if sys.argv[1] in ['-h', '--help']:
parser.print_help()
elif sys.argv[1] in ['-v', '--version']:
print(f'{__version__}')
elif len(sys.argv) == 3:
if sys.argv[1] in ['-V', '--view']:
print(args.view_name)
subprocess.call(f'python ./job_version.py {args.view_name}', shell=True)

Command run:

1
E:\ python .\cli.py
usage: cli.py [-h] [-v] [-V VIEW_NAME]

Manage Jenkins Job

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         Show version
  -V VIEW_NAME, --view VIEW_NAME
                        Get Jenkins job last build number by view

Get job number:

1
PS E:\neolink_tools> python .\cli.py -V jenkins view name
{
    "a": 12,
    "b": 17,
    "c": 20,
    "d": 37
}
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!