-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargumentparser.py
99 lines (74 loc) · 3.16 KB
/
argumentparser.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import argparse
import os, sys
from classes import *
from generatelstlisting import version
description = """\
Create latex from git diff supplied in FILE or standard input.
Make sure it does not contain coloring: add option --no-color to git diff cmd
"""
epilog = """\
Make sure to include the following in your preamble:
\\usepackage{array}
\\usepackage{tabu}
\\usepackage{longtable}
\\usepackage[table]{xcolor}
"""
defaultFilename = "generated-lstlisting.tex"
defaultSection = "sub"
mapping = {
"part":"part",
"sec":"section",
"sub":"subsection",
"subsub":"subsubsection",
"par":"paragraph"
}
sectionChoices = ["part", "sec", "sub", "subsub", "par"]
parser = argparse.ArgumentParser(usage='%(prog)s [OPTION ...] DIR', description=description, epilog=epilog,
formatter_class=argparse.RawDescriptionHelpFormatter,)
parser.add_argument(dest='indir', metavar="DIR", # nargs="?",
help='input directory')
parser.add_argument('--version', action='version',
version='%(prog)s version {}'.format(version) )
parser.add_argument('-o', dest='outfile', metavar="FILE", default = defaultFilename,
help='place the output into FILE (default: {})'.format(defaultFilename) )
group = parser.add_mutually_exclusive_group()
group.add_argument('--section', dest='section', default = defaultSection,
choices=sectionChoices,
help='''section level for file title -- will also set label prefix
-- (default: {})'''.format(defaultSection) )
group.add_argument('--custom-section', dest='customsection', metavar="STR",
help='custom section level for file title' )
parser.add_argument('--custom-label', dest='customlabel', metavar="STR",
help='custom label prefix for file title' )
parser.add_argument('--no-label', dest='nolabel', action="store_const",
const=True, default=False,
help="don't create labels for file title")
parser.add_argument('-q', '--quiet', dest='quiet', action="store_const",
const=True, default=False,
help="don't print messages")
def parse_cli():
args = parser.parse_args()
if args.outfile is None:
settings.outfile = defaultFilename
else:
dirname = os.path.dirname(args.outfile)
if dirname != "" and not os.path.isdir( dirname ):
print("output file is not in an existing folder")
sys.exit(2)
settings.outfile = args.outfile
if args.customsection:
settings.headingStyle = args.customsection
else:
settings.labelPrefix = args.section
settings.headingStyle = mapping[args.section]
if args.customlabel:
settings.labelPrefix = args.customlabel + ":"
elif args.customsection and not args.nolabel:
print("Custom section level, but no custom label prefix, prefix will not be used")
settings.labelPrefix = ""
settings.shouldAddLabel = not args.nolabel
if not os.path.isdir(args.indir):
print("%s does not exist" % args.indir)
sys.exit(2)
settings.quiet = args.quiet
return (args.indir)