Monday, May 17, 2021

Pyhon Argument Parsers

CLICK 
-----

 

Using subcommands 

@click.group() 

@click.option('--manage/--no-manage', default=False) 

def cli(manage): 

click.echo('What to manage?') 

 

 

@click.option('-u', required=True, help='user name') 

@cli.command() 

def a10(u): 

click.echo('Do a10 stuffs here') 

click.echo('Username: {}'.format(u)) 

 

 

@click.option('-u', required=True, help='user name') 

@click.option('-a', help='aflexname') 

@cli.command() 

def aflex(u, a): 

click.echo('Do aflex stuffs here') 

click.echo('Aflex name is: {}'.format(a)) 

click.echo('Username: {}'.format(u)) 

 

source: 

 

SYS 

--- 

 

Basics 

This module provides `argv` which handles command line arguments. For example, 

~$ python -c "import sys; print(sys.argv)" one 2 3rd 

['-c', 'one', '2', '3rd'] 

~$  

 
The command line arguments above were stored as follows: 

~$ python -c "import sys; print(sys.argv[0])" one 2 3rd 

-c 

~$ python -c "import sys; print(sys.argv[1])" one 2 3rd 

one 

~$ python -c "import sys; print(sys.argv[2])" one 2 3rd 

2 

~$ python -c "import sys; print(sys.argv[3])" one 2 3rd 

3rd 

~$  
 
Other related modules are: 

`getopt`   - processes sys.argv using Unix getopt() function 
 

No comments:

Post a Comment