-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
executable file
·68 lines (57 loc) · 1.85 KB
/
__main__.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Entry point(s) for the command line interface"""
from __future__ import absolute_import, print_function
from bragly.cli import parse_args, parse_utility_args
import sys
def main(args=None):
"""Either takes arguments or gets them from sys.argv. Calls the bragly.cli
parsing function parse_args, and then executes the returned function.
See the usage at the command line by passing --help as an option.
Args:
args (list): A list of arguments that would be in sys.argv. If None,
args will be populated from sys.argv.
Returns:
int:
0 - If the program runs without exception.
1 - If the program runs but there is an unexpected exception
2 - If the program runs and no arguments are parsed form the command
line.
"""
if args is None:
args = sys.argv[1:]
parser, args = parse_args(args)
if not args:
parser.print_help()
return 2
func = args.pop('func', None)
if func is None:
print("Operation failed for unexpected reason")
return 1
results = func(**args)
for result in results:
print(result, flush=True)
return 0
def util(args=None):
"""Entry point for brag utilities.
Args:
args (list): A list of arguments that would be in sys.argv. If None,
args will be populated from sys.argv.
Returns:
"""
if args is None:
args = sys.argv[1:]
parser, args = parse_utility_args(args)
if not args:
parser.print_help()
return 2
func = args.pop('func', None)
if func is None:
print("Operation failed for unexpected reason")
return 1
results = func(**args)
for result in results:
print(result, flush=True)
return 0
if __name__ == '__main__':
main()