python argparse - either both optional arguments or else neither one

后端 未结 3 1274
耶瑟儿~
耶瑟儿~ 2020-12-29 04:25

I have a program that uses a default name and password. I\'m using argparse to allow the user to specify command line options, and I would like to enable the user to provid

相关标签:
3条回答
  • 2020-12-29 04:59

    I believe that the best way to handle this is to post-process the returned namespace. The reason that argparse doesn't support this is because it parses arguments 1 at a time. It's easy for argparse to check to see if something was already parsed (which is why mutually-exclusive arguments work), but it isn't easy to see if something will be parsed in the future.

    A simple:

    parser.add_argument('-n','--name',...,default=None)
    parser.add_argument('-p','--password',...,default=None)
    ns = parser.parse_args()
    
    if len([x for x in (ns.name,ns.password) if x is not None]) == 1:
       parser.error('--name and --password must be given together')
    
    name = ns.name if ns.name is not None else "default_name"
    password = ns.password if ns.password is not None else "default_password"
    

    seems like it would suffice.

    0 讨论(0)
  • 2020-12-29 05:01

    This is probably how I'd do it. Since you have existing defaults with the option to change, define the defaults, but don't use them as your argument defaults:

    default_name = "x"
    default_pass = "y"
    parser.add_argument(
        '-n',
        '--name',
        default=None,
        help='the login name that you wish the program to use'
        )
    
    parser.add_argument(
        '-p',
        '--password',
        default=None,
        help='the password to log in with.'
        )
    args = parser.parse_args()
    if all(i is not None for i in [args.name, args.password]):
        name = args.name
        passwd = args.password
    elif any(i is not None for i in [args.name, args.password]):
        parser.error("Both Name and Password are Required!")
    else:
        name = default_name
        passwd = default_pass
    
    0 讨论(0)
  • 2020-12-29 05:02

    I know this is more than two years late, but I found a nice and concise way to do it:

    if bool(ns.username) ^ bool(ns.password):
        parser.error('--username and --password must be given together')
    

    ^ is the XOR operator in Python. To require both arguments given at the command line is essentially an XOR test.

    0 讨论(0)
提交回复
热议问题