Python argparse static arguments -


i'm new argparse in python.

my program can reset create, delete or reset password of account.

python manager.py create  <some_username>       # create account                   delete  <some_username>       # delete account                   resetpw <some_username> mysql # reset mysql pw of account                   resetpw <some_username> unix  # reset unix pw of account 

the arguments "create", "delete", "mysql" , "unix" static. how can implement in argparse?

i've tried following:

parser = argparse.argumentparser(prog='manager') parser.add_argument('create', action='store_true', help='create account') parser.add_argument('delete', action='store_true', help='delete account') args = parser.parse_args() 

but doesn't work.

you're looking subparsers:

import argparse  parser = argparse.argumentparser(prog='manager') sub = parser.add_subparsers(dest='command') sub.add_parser('create', help='create account') sub.add_parser('delete', help='delete account')  print parser.parse_args(['create']) # namespace(command='create') 

as side note, i'd recommend try plac or argh. provide simpler , more intuitive syntax standard argparse


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -