2

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).

Foobar
  • 7,458
  • 16
  • 81
  • 161

1 Answers1

2
parser.add_argument('foobar', choices=['start', 'stop'], 
    help='a positional that accepts one of two words') 
hpaulj
  • 221,503
  • 14
  • 230
  • 353