Python: argparse optional arguments without dashes

前端 未结 3 1084
终归单人心
终归单人心 2021-01-01 12:58

I would like to have the following syntax:

python utility.py file1 FILE1 file2 FILE2

where file1 and file2 are optional arguments. It is si

相关标签:
3条回答
  • 2021-01-01 13:42

    Another Example would be:

    train.py
    
    import argparse
    if __name__ == '__main__':
        parser = argparse.ArgumentParser(description="Arguments for wake_word")
        parser.add_argument('data', type=str, help="path to data")
        parser.add_argument('output', type=str, help="model save path")
        parser.add_argument('batch_size', type=int, help="batch size")
        parser.add_argument('epochs', type=int, help="no.s of epochs")
        args = parser.parse_args()
    print(args.data + args.output + args.batch_size + args.epochs)
    

    then you can just run this code with arguments without dash

    train.py /path/to/data/ /path/to/output_files/ 128 100
    

    And, in ascending order

    0 讨论(0)
  • 2021-01-01 13:43

    There is no way to get argparse to do this for you. However, you can make argparse accept any number of positional arguments:

    parser.add_argument('FILES',nargs='*')
    options=parser.parse_args()
    file1,optional_files=options.FILES[0],options.FILES[1:]
    

    Of course, you may want to add some checks to make sure that at least 1 file was given, etc.

    EDIT

    I'm still not 100% sure what you want here, but if file1 and file2 are literal strings, you can work around that a little bit by preprocessing sys.argv. Of course, this will still format your help message strangely, but you can always add an epilog explaining that either form is OK:

    import argparse
    import sys
    
    mangle_args=('file1','file2')
    arguments=['--'+arg if arg in mangle_args else arg for arg in sys.argv[1:]]
    
    parser=argparse.ArgumentParser()
    parser.add_argument('--file1')
    parser.add_argument('--file2')
    options=parser.parse_args(arguments)
    
    0 讨论(0)
  • 2021-01-01 13:43

    Had same problem. My workaround is:

    lastarg = sys.argv[-1]
    if len(sys.argv) > 1 and lastarg[0] != '-':
        sys.argv[-1] = '-file'
        sys.argv.append(lastarg)
    
    argparser = argparse.ArgumentParser()
    argparser.add_argument('-d', action='store_true')
    argparser.add_argument('-nrv', action='store_true')
    argparser.add_argument('-file', type=str, default='')
    args = argparser.parse_args()
    
    0 讨论(0)
提交回复
热议问题