Python argparse: Make at least one argument required

前端 未结 11 1596
谎友^
谎友^ 2020-12-13 01:39

I\'ve been using argparse for a Python program that can -process, -upload or both:

parser = argparse.ArgumentParser(de         


        
相关标签:
11条回答
  • 2020-12-13 02:01
    args = vars(parser.parse_args())
    if not any(args.values()):
        parser.error('No arguments provided.')
    
    0 讨论(0)
  • 2020-12-13 02:02

    The best way to do this is by using python inbuilt module add_mutually_exclusive_group.

    parser = argparse.ArgumentParser(description='Log archiver arguments.')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-process', action='store_true')
    group.add_argument('-upload',  action='store_true')
    args = parser.parse_args()
    

    If you want only one argument to be selected by command line just use required=True as an argument for group

    group = parser.add_mutually_exclusive_group(required=True)
    
    0 讨论(0)
  • 2020-12-13 02:03

    If you require a python program to run with at least one parameter, add an argument that doesn't have the option prefix (- or -- by default) and set nargs=+ (Minimum of one argument required). The problem with this method I found is that if you do not specify the argument, argparse will generate a "too few arguments" error and not print out the help menu. If you don't need that functionality, here's how to do it in code:

    import argparse
    
    parser = argparse.ArgumentParser(description='Your program description')
    parser.add_argument('command', nargs="+", help='describe what a command is')
    args = parser.parse_args()
    

    I think that when you add an argument with the option prefixes, nargs governs the entire argument parser and not just the option. (What I mean is, if you have an --option flag with nargs="+", then --option flag expects at least one argument. If you have option with nargs="+", it expects at least one argument overall.)

    0 讨论(0)
  • 2020-12-13 02:04

    I know this is old as dirt, but the way to require one option but forbid more than one (XOR) is like this:

    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-process', action='store_true')
    group.add_argument('-upload',  action='store_true')
    args = parser.parse_args()
    print args
    

    Output:

    >opt.py  
    usage: multiplot.py [-h] (-process | -upload)  
    multiplot.py: error: one of the arguments -process -upload is required  
    
    >opt.py -upload  
    Namespace(process=False, upload=True)  
    
    >opt.py -process  
    Namespace(process=True, upload=False)  
    
    >opt.py -upload -process  
    usage: multiplot.py [-h] (-process | -upload)  
    multiplot.py: error: argument -process: not allowed with argument -upload  
    
    0 讨论(0)
  • 2020-12-13 02:04

    This achieves the purpose and this will also be relfected in the argparse autogenerated --help output, which is imho what most sane programmers want (also works with optional arguments):

    parser.add_argument(
        'commands',
        nargs='+',                      # require at least 1
        choices=['process', 'upload'],  # restrict the choice
        help='commands to execute'
    )
    

    Official docs on this: https://docs.python.org/3/library/argparse.html#choices

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