Skip to content

Commit 33ff79f

Browse files
committed
Transform/DDG: Use argparse to parse the arguments
1 parent a8e617c commit 33ff79f

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

index2ddg.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'''
2020

2121
import sys, json, os, sys, re, fnmatch
22+
import argparse
2223
import lxml.etree as e
2324
import lxml.html as html
2425

@@ -409,29 +410,30 @@ def process_identifier(out, redirects, root, link, item_ident, item_type,
409410
out.write(line)
410411

411412
def main():
412-
if len(sys.argv) != 3 and not (len(sys.argv) > 2 and sys.argv[2] == 'debug'):
413-
print('''Please provide the file name of the index as the first argument
414-
and the file name of the output as the second ''')
415-
sys.exit(1)
416413

417-
MAX_CODE_LINES = 6
414+
parser = argparse.ArgumentParser(prog='index2ddg.py')
415+
parser.add_argument('index', type=str,
416+
help='The path to the XML index containing identifier data')
417+
parser.add_argument('output', type=str,
418+
help='The path to destination output.txt file')
419+
parser.add_argument('--debug', action='store_true', default=False,
420+
help='Enables debug mode.')
421+
parser.add_argument('--debug_ident', type=str, default=None,
422+
help='Processes only the identifiers that match debug_ident')
423+
args = parser.parse_args()
418424

419425
# If a the second argument is 'debug', the program switches to debug mode and
420426
# prints everything to stdout. If the third argument is provided, the program
421427
# processes only the identifiers that match the provided string
422428

423-
debug = False
424-
debug_ident = None
425-
if len(sys.argv) > 2 and sys.argv[2] == 'debug':
426-
debug = True
427-
if len(sys.argv) > 3:
428-
debug_ident = sys.argv[3]
429+
debug = args.debug
430+
debug_ident = args.debug_ident
429431

430432
# track the statistics of number of lines used by the entries
431433
debug_num_lines = [0 for i in range(40)]
432434

433-
index_file = sys.argv[1]
434-
output_file = sys.argv[2]
435+
index_file = args.index
436+
output_file = args.output
435437

436438
# a map that stores information about location and type of identifiers
437439
# it's two level map: full_link maps to a dict that has full_name map to

0 commit comments

Comments
 (0)