简体   繁体   中英

How to parse command line arguments with flags and without

I typically use optparse/argparse to grab command line arguments but they always have a flag associated with them. (I have to write some stuff to work on python pre 2.6 and also post 2.6)

./myscript -d -v -f something

However, I now need to do something like this:

./myscript -d -v -f something -g something action

Where action is supplied without a -flag in front. (Sorry can't use a flag here, silly group I'm interfacing with don't wanna change their code)

I'd like to still use optparse/argparse but also grab the action argument. There is a variable number of -flag arguments that can be provided and only 1 bare action argument.

要创建位置参数,只是不要在名称前加一个连字符,即

parser.add_argument('action')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM