Skip to content

Commit 6819b3a

Browse files
committed
Fix parsing of endpoint
Fixes #75
1 parent 26920e8 commit 6819b3a

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

jgo/jgo.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,6 @@ def run_and_combine_outputs(command, *args):
263263
return subprocess.check_output(command_string, stderr=subprocess.STDOUT)
264264

265265

266-
def find_endpoint(argv, shortcuts={}):
267-
# endpoint is first positional argument
268-
pattern = re.compile(".*https?://.*")
269-
indices = []
270-
for index, arg in enumerate(argv):
271-
if arg in shortcuts or (Endpoint.is_endpoint(arg) and not pattern.match(arg)):
272-
indices.append(index)
273-
return -1 if len(indices) == 0 else indices[-1]
274-
275-
276266
_default_log_levels = (
277267
"NOTSET",
278268
"DEBUG",
@@ -308,6 +298,7 @@ def jgo_parser(log_levels=_default_log_levels):
308298
"""
309299

310300
parser = argparse.ArgumentParser(
301+
prog="jgo",
311302
description="Run Java main class from Maven coordinates.",
312303
usage=usage[len("usage: ") :],
313304
epilog=epilog,
@@ -376,6 +367,18 @@ def jgo_parser(log_levels=_default_log_levels):
376367
parser.add_argument(
377368
"--log-level", default=None, type=str, help="Set log level", choices=log_levels
378369
)
370+
parser.add_argument(
371+
"endpoint",
372+
help="Endpoint",
373+
metavar="<endpoint>",
374+
)
375+
parser.add_argument(
376+
"program_args",
377+
help="Program arguments",
378+
metavar="main-args",
379+
nargs="*",
380+
default=[],
381+
)
379382

380383
return parser
381384

@@ -398,7 +401,6 @@ def _jgo_main(argv=sys.argv[1:], stdout=None, stderr=None):
398401
completed_process.check_returncode()
399402

400403
except HelpRequested:
401-
pass
402404
parser.print_help()
403405

404406
except NoEndpointProvided:
@@ -717,15 +719,18 @@ def run(parser, argv=sys.argv[1:], stdout=None, stderr=None):
717719
repositories = config["repositories"]
718720
shortcuts = config["shortcuts"]
719721

720-
endpoint_index = find_endpoint(argv, shortcuts)
721-
if endpoint_index == -1:
722-
raise HelpRequested(
723-
argv
724-
) if "-h" in argv or "--help" in argv else NoEndpointProvided(argv)
722+
if "-h" in argv or "--help" in argv:
723+
raise HelpRequested(argv)
724+
725+
args, unknown = parser.parse_known_args(argv)
726+
727+
if not args.endpoint:
728+
raise NoEndpointProvided(argv)
729+
if args.endpoint in shortcuts and not Endpoint.is_endpoint(args.endpoint):
730+
raise NoEndpointProvided(argv)
725731

726-
args, unknown = parser.parse_known_args(argv[:endpoint_index])
727732
jvm_args = unknown if unknown else []
728-
program_args = [] if endpoint_index == -1 else argv[endpoint_index + 1 :]
733+
program_args = args.program_args
729734
if args.log_level:
730735
logging.getLogger().setLevel(logging.getLevelName(args.log_level))
731736

@@ -755,7 +760,7 @@ def run(parser, argv=sys.argv[1:], stdout=None, stderr=None):
755760
if args.force_update:
756761
args.update_cache = True
757762

758-
endpoint_string = "+".join([argv[endpoint_index]] + args.additional_endpoints)
763+
endpoint_string = "+".join([args.endpoint] + args.additional_endpoints)
759764

760765
primary_endpoint, workspace = resolve_dependencies(
761766
endpoint_string,

tests/test_main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
3+
from jgo.jgo import jgo_parser
4+
5+
6+
class ArgParserTest(unittest.TestCase):
7+
def test_program_arg_path(self):
8+
parser = jgo_parser()
9+
argv = ["mvxcvi:cljstyle", "fix", "/c/path/to/file.clj"]
10+
11+
args = parser.parse_args(argv)
12+
self.assertEqual(args.endpoint, "mvxcvi:cljstyle")
13+
self.assertEqual(args.program_args, ["fix", "/c/path/to/file.clj"])
14+
15+
def test_program_arg_path_windows_drive(self):
16+
parser = jgo_parser()
17+
argv = ["mvxcvi:cljstyle", "fix", "c:/path/to/file.clj"]
18+
19+
args = parser.parse_args(argv)
20+
self.assertEqual(args.endpoint, "mvxcvi:cljstyle")
21+
self.assertEqual(args.program_args, ["fix", "c:/path/to/file.clj"])
22+
23+
def test_program_arg_path_windows_sep(self):
24+
parser = jgo_parser()
25+
argv = ["mvxcvi:cljstyle", "fix", "c:\\path\\to\\file.clj"]
26+
27+
args = parser.parse_args(argv)
28+
self.assertEqual(args.endpoint, "mvxcvi:cljstyle")
29+
self.assertEqual(args.program_args, ["fix", "c:\\path\\to\\file.clj"])
30+
31+
def test_jvm_args(self):
32+
parser = jgo_parser()
33+
argv = ["-Xms1G", "mvxcvi:cljstyle", "fix", "c:\\path\\to\\file.clj"]
34+
35+
args, unknown = parser.parse_known_args(argv)
36+
self.assertEqual(args.endpoint, "mvxcvi:cljstyle")
37+
self.assertEqual(args.program_args, ["fix", "c:\\path\\to\\file.clj"])
38+
self.assertEqual(unknown, ["-Xms1G"])
39+
40+
41+
if __name__ == "__main__":
42+
unittest.main()

0 commit comments

Comments
 (0)