Skip to content

Commit df1ef37

Browse files
committed
Improve command line util
1 parent b1045d6 commit df1ef37

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

switch_model/__main__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def version():
1717
print(f"Switch Git branch: {branch}")
1818
return 0
1919

20+
def help_text():
21+
print(
22+
f"Must specifiy one of the following commands: {list(cmds.keys())}.\nE.g. Run 'switch solve' or 'switch get_inputs'.")
23+
24+
2025
def get_module_runner(module):
2126
def runner():
2227
importlib.import_module(module).main()
@@ -29,18 +34,20 @@ def runner():
2934
"test": get_module_runner("switch_model.test"),
3035
"upgrade": get_module_runner("switch_model.upgrade"),
3136
"get_inputs": get_module_runner("switch_model.wecc.get_inputs"),
32-
"version": version,
3337
"drop": get_module_runner("switch_model.tools.drop"),
3438
"new": get_module_runner("switch_model.tools.new"),
3539
"graph": get_module_runner("switch_model.tools.graph.cli_graph"),
3640
"compare": get_module_runner("switch_model.tools.graph.cli_compare"),
3741
"db": get_module_runner("switch_model.wecc.__main__"),
42+
"help": help_text
3843
}
3944

4045

4146
def main():
4247
parser = argparse.ArgumentParser(add_help=False)
43-
parser.add_argument("subcommand", choices=cmds.keys(), help="The possible switch subcommands")
48+
parser.add_argument("--version", default=False, action="store_true", help="Get version info")
49+
parser.add_argument("subcommand", choices=cmds.keys(), help="The possible switch subcommands", nargs="?",
50+
default="help")
4451

4552
# If users run a script from the command line, the location of the script
4653
# gets added to the start of sys.path; if they call a module from the
@@ -53,9 +60,13 @@ def main():
5360

5461
args, remaining_args = parser.parse_known_args()
5562

63+
if args.version:
64+
return version()
65+
5666
# adjust the argument list to make it look like someone ran "python -m <module>" directly
57-
sys.argv[0] += " " + sys.argv[1]
58-
del sys.argv[1]
67+
if len(sys.argv) > 1:
68+
sys.argv[0] += " " + sys.argv[1]
69+
del sys.argv[1]
5970
cmds[args.subcommand]()
6071

6172

0 commit comments

Comments
 (0)