-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalloc
executable file
·40 lines (36 loc) · 1.33 KB
/
alloc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
import sys
from alloccli import alloc
# Try to import relevant module
try:
# Get the module name from the passed options
module = sys.argv[1]
#
# "Note: This is an advanced function that is not needed in everyday Python
# programming, unlike importlib.import_module()."
#
# Here we import the module.
acli = __import__("alloccli." + module)
# Now we get the class from the module, e.g.: alloccli.task.task
subcommand = getattr(getattr(acli, module), module)
subcommand().run(sys.argv)
# NOTE:: we must use `except Exception:` so the help does not get called
# everytime a module calls sys.exit()
except Exception as e:
# Check if there is an option
if len(sys.argv) > 1:
# if there is an option, see if it is asking for help
if "-h" in sys.argv[1] or "--help" in sys.argv[1] or "help" in sys.argv[1]:
alloc.alloc().get_cli_help()
# else it is an unkown option
else:
sys.stderr.write(
"!!! Invalid command: "
+ sys.argv[1]
+ "\nPlease pass --help for help\n"
)
# we could suppress the error, but it is arguably better to spill it.
raise(e)
# If no option was passed to alloc, print the help.
else:
alloc.alloc().get_cli_help()