Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99297 > unrolled thread
| Started by | <c.buhtz@posteo.jp> |
|---|---|
| First post | 2015-11-23 18:04 +0100 |
| Last post | 2015-11-23 18:04 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
[argparse] optional parameter without --switch <c.buhtz@posteo.jp> - 2015-11-23 18:04 +0100
| From | <c.buhtz@posteo.jp> |
|---|---|
| Date | 2015-11-23 18:04 +0100 |
| Subject | [argparse] optional parameter without --switch |
| Message-ID | <mailman.88.1448345018.2291.python-list@python.org> |
I want to call (on bash) a Python script in this two ways without any
error.
./arg.py
./arg.py TEST
It means that the parameter (here with the value `TEST`) should be
optional. With argparse I only know a way to create optional paramters
when they have a switch (like `--name`).
Is there a way to fix that?
#!/usr/bin/env python3
import sys
import argparse
parser = argparse.ArgumentParser(description=__file__)
# must have
#parser.add_argument('name', metavar='NAME', type=str)
# optional BUT with a switch I don't want
#parser.add_argument('--name', metavar='NAME', type=str)
# store all arguments in objects/variables of the local namespace
locals().update(vars(parser.parse_args()))
print(name)
sys.exit()
--
GnuPGP-Key ID 0751A8EC
Back to top | Article view | comp.lang.python
csiph-web