python - argparse - Different mandatory / available parameters per action -


i'm looking create argument parser following structure:

options [ 'backup', 'consistency_check', 'backup_and_consistency_check']  --database [ required ] --action [ required choice list options ]   --where_to_backup_to [ required if --action 'backup' ]   --what_file_to_consistency_check [ required if --action 'consistency_check'] --clean [ optional ]   --force [ optional if --clean in arguments ] 

how can implement optional arguments using argumentparser module, depending on choice made command line argument.

i'm looking make argparse fail if example command line arguments are

--d database_name --a backup --what_file_to_consistency_check /var/tmp/file.bak 

this i've gotten far (i know it's little don't want go in complete wrong direction subparsers if haven't gotten right start)

actions = ['backup', 'consistency_check', 'backup_and_consistency_check']  def create_parser():     parser = argumentparser(description='parser backup / consistency check')      parser.add_argument('--database', '-d', dest='db', help='database name', choices=get_active_database_list())      parser.add_argument('--action', '-a', dest='action', help='action option', choices=actions)     # --where_to_backup_to [ if action = backup ]     # --what_file_to_consistency_check [ if action = cc ]     parser.add_argument('--clean', '-c', dest='clean', help='clean')     # --force [ available if --clean in arguments ]      return parser 

if subparsers seem complex @ moment, think can still useful parser without them:

def create_parser():     parser = argumentparser(description='parser backup / consistency check')      parser.add_argument('--database', '-d', dest='db', help='database name', choices=get_active_database_list())      parser.add_argument('--action', '-a', help='action option', choices=actions)     parser.add_argument('target', help='target backup or check')     parser.add_argument('--clean', '-c', help='clean') # default dest 'clean'     parser.add_argument('--force', help='force clean')     return parser 

if database required, might want add required=true parameter it. or make positional. otherwise consider if user not supply it. i.e. if args.db none? there default database can use?

it looks action choices require file or dir argument - target backup or checking. matter whether user calls '--where_to_backup_to' or '--what_file_to_consistency_check'? using positional here i'm requiring them give sort of name, it's interpret according 'action'.

it looks force stronger version of clean. think user wants if specify --force not --clean? here accept both , let code choose makes sense.

my philosophy primary goal of parser figuring out user wants. error checking useful when prevents ambiguous input. shouldn't picky. simple parser design better overly complex one.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -