Skip to content

Commit

Permalink
fix positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyao-lin committed Sep 15, 2019
1 parent 6d06ced commit d0b5daa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
24 changes: 19 additions & 5 deletions flagset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
assert (
cmdline_name is not None or env_name is not None or config_name is not None
), "expecting at least one flag name"

assert (
default is None or not required
), "cannot provide default value for required flag"
Expand All @@ -70,6 +71,9 @@ def __init__(
self.default = default
self.help = help
self.required = required
self.positional = isinstance(cmdline_name, str) and not cmdline_name.startswith(
"-"
)

def _parse_env(self, env):
if self.env_name is not None and self.env_name in env:
Expand All @@ -87,7 +91,7 @@ def _bind_argparser(self, parser, dest=None):

kwargs = {}

if dest is not None:
if dest is not None and not self.positional:
kwargs["dest"] = dest

# help message: <help>[. default value '<default>'][. env var '<env>']
Expand Down Expand Up @@ -157,8 +161,6 @@ class FlagSet:
5. optional help message
"""

INTERNAL_FLAG_CONFIG = "config"

def __init__(self, init_set={}):
"""
:params config_parser: should take a string and return dict
Expand All @@ -179,7 +181,6 @@ def _parse_cmdline(self, args):
res = {}

parser = _ArgumentParserWithException(add_help=False)
parser.add_argument("__config", nargs="?")
parser.add_argument(
"--help", "-h", dest="__help", action="store_true", default=False
)
Expand All @@ -190,8 +191,20 @@ def _parse_cmdline(self, args):
if flag.cmdline_name is not None:
flag._bind_argparser(parser, canon)

# bind config flag last
parser.add_argument("__config", nargs="?")

parsed = vars(parser.parse_args(args))

for canon, flag in self.flags.items():
if (
flag.positional
and flag.cmdline_name in parsed
and canon != flag.cmdline_name
):
parsed[canon] = parsed[flag.cmdline_name]
del parsed[flag.cmdline_name]

if parsed["__help"]:
raise FlagHelp()

Expand Down Expand Up @@ -262,11 +275,12 @@ def parse(

def print_help(self):
parser = argparse.ArgumentParser()
parser.add_argument("config", nargs="?", help="config file")

for flag in self.flags.values():
flag._bind_argparser(parser)

parser.add_argument("config", nargs="?", help="config file")

parser.print_help(file=sys.stderr)

# show other flags
Expand Down
11 changes: 11 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ def test_precedence(self):
use_exc=True,
)
self.assertEqual(flags["flag"], "from cmd")

def test_positional(self):
fset = FlagSet(
{"flag": Flag(cmdline_name="flag-name")}
)

flags = fset.parse(
args=["from_cmd"], use_exc=True
)
self.assertEqual(flags["flag"], "from_cmd")
self.assertNotIn("flag-name", flags)

0 comments on commit d0b5daa

Please sign in to comment.