Foobar
Foobar

Reputation: 8467

argparse - optional argument without prefix

Related:

Using argparse for mandatory argument without prefix

Python: argparse optional arguments without dashes

I need a simple script that accepts either ./x start or ./x stop. And start and stop should have their own entries in the help menu.

This doesn't seem super-doable with argparse.

I get close with

parser.add_argument('--start', action='store_true', help='stuff')
parser.add_argument('--stop', action='store_true', help='stuff2')

but, this requires preceding dashes before each argument.

Is there a way to do this without sub-commands? (Especially since bothstart and stop accept the same set of optional flags).

Upvotes: 2

Views: 269

Answers (1)

hpaulj
hpaulj

Reputation: 231385

parser.add_argument('foobar', choices=['start', 'stop'], 
    help='a positional that accepts one of two words') 

Upvotes: 2

Related Questions