From 47413b75725b6ee3062a59f0e4b73ae723b3e92f Mon Sep 17 00:00:00 2001 From: Tyler Cipriani Date: Fri, 19 Jul 2019 08:16:17 -0600 Subject: fcc-codegen: allow '--' in "markdown" argument By Using the PARSER (i.e., "A...") argument for nargs, '--' becomes an allowed value in the argument list. This should resolve the TODO comment in the file. There is a test funciton in this file as well (I wasn't sure where to put the test, but it's working). --- ftt-codegen | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'ftt-codegen') diff --git a/ftt-codegen b/ftt-codegen index bc2d28a..9f2644a 100755 --- a/ftt-codegen +++ b/ftt-codegen @@ -136,18 +136,18 @@ def infer_basename(markdowns): return root -# This only allows one markdown filename. This is crap. But I can't -# get it work with -- otherwise. What I want is for the following to -# work: -# ftt-codegen --pdf foo.md bar.md -- --test-arg --other-test-arg -# but I can't make it work. -def parse_cli(): +# Usage example: +# ftt-codegen foo.md bar.md -- --test-arg --other-test-arg +def parse_cli(argv=None): p = argparse.ArgumentParser() p.add_argument('--run', action='store_true') - p.add_argument('markdown', nargs=1) - args, more_args = p.parse_known_args() - if more_args and more_args[0] == '--': - more_args = more_args[1:] + p.add_argument('markdown', nargs='A...') + args = p.parse_args(argv) + more_args = [] + if '--' in args.markdown: + break_index = args.markdown.index('--') + more_args = args.markdown[break_index + 1:] + args.markdown = args.markdown[:break_index] return args, more_args @@ -166,6 +166,16 @@ def debug(msg): sys.stderr.write('DEBUG: {}\n'.format(msg)) sys.stderr.flush() +def test_parse_cli(): + args, more_args = parse_cli([ + 'foo.md', 'bar.md', '--', '--test-arg', '--other-test-arg' + ]) + assert args.markdown == ['foo.md', 'bar.md'] + assert more_args == ['--test-arg', '--other-test-arg'] + + args, more_args = parse_cli(['foo.md']) + assert args.markdown == ['foo.md'] + assert not more_args args, test_args = parse_cli() -- cgit v1.2.1