Skip to content

Commit

Permalink
Improve argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Sep 14, 2018
1 parent d25eea9 commit 4bcf109
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
43 changes: 41 additions & 2 deletions julia/pseudo_python_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,51 @@ def make_parser(description=__doc__):
return parser


def parse_args_with(parser, args):
ns = parser.parse_args(list(preprocess_args(args)))
if (ns.command or ns.module) and ns.script:
ns.args = [ns.script] + ns.args
ns.script = None
return ns


def parse_args(args):
return parse_args_with(make_parser(), args)


def preprocess_args(args):
"""
Insert "--" after "[-c cmd | -m mod | script | -]"
This is required for the following to work:
>>> ns = parse_args(["-mjson.tool", "-h"])
>>> ns.args
['-h']
"""
it = iter(args)
for a in it:
yield a

if a in ("-m", "-c"):
try:
yield next(it)
except StopIteration:
return
yield "--"
elif a == "-":
yield "--"
elif a.startswith("-"):
if a[1] in ("m", "c"):
yield "--"
# otherwise, it's some


def main(args=None):
if args is None:
args = sys.argv[1:]
parser = make_parser()
try:
ns = parser.parse_args(args)
ns = parse_args(args)
python(**vars(ns))
except SystemExit as err:
return err.code
Expand Down
4 changes: 2 additions & 2 deletions julia/python_jl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import os
import sys

from .pseudo_python_cli import make_parser
from .pseudo_python_cli import make_parser, parse_args_with

script_jl = """
import PyCall
Expand Down Expand Up @@ -106,7 +106,7 @@ def parse_pyjl_args(args):
Julia interpreter to be used.
""")

ns = parser.parse_args(args)
ns = parse_args_with(parser, args)
unused_args = list(remove_julia_options(args))
return ns, unused_args

Expand Down
45 changes: 45 additions & 0 deletions test/test_pseudo_python_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import shlex

import pytest

from julia.pseudo_python_cli import parse_args


def make_dict(**kwargs):
ns = parse_args([])
return dict(vars(ns), **kwargs)


@pytest.mark.parametrize("args, desired", [
("-m json.tool -h", make_dict(module="json.tool", args=["-h"])),
("-mjson.tool -h", make_dict(module="json.tool", args=["-h"])),
("-m ipykernel install --user --name NAME --display-name DISPLAY_NAME",
make_dict(module="ipykernel",
args=shlex.split("install --user --name NAME"
" --display-name DISPLAY_NAME"))),
("-m ipykernel_launcher -f FILE",
make_dict(module="ipykernel_launcher",
args=shlex.split("-f FILE"))),
])
def test_parse_args(args, desired):
ns = parse_args(shlex.split(args))
actual = vars(ns)
assert actual == desired


@pytest.mark.parametrize("cli_args", [
["-h"],
["-i", "--help"],
["-h", "-i"],
["-hi"],
["-ih"],
["-h", "-m", "json.tool"],
["-h", "-mjson.tool"],
])
def test_help_option(cli_args, capsys):
with pytest.raises(SystemExit) as exc_info:
parse_args(cli_args)
assert exc_info.value.code == 0

captured = capsys.readouterr()
assert "usage:" in captured.out

0 comments on commit 4bcf109

Please sign in to comment.