Cleaned up argument parsing

This commit is contained in:
Dmytro Yeroshkin 2021-10-23 14:08:12 +02:00
parent fc6ffac5a9
commit 5df3a48f35
1 changed files with 28 additions and 10 deletions

View File

@ -1,10 +1,9 @@
#!/usr/bin/python #!/usr/bin/python
import collections
import sys
import tempfile
import argparse import argparse
import collections
import tempfile
CHAPTER_MARKER = '## ' CHAPTER_MARKER = '## '
STATUS_MARKER = '[status]: # ' STATUS_MARKER = '[status]: # '
@ -28,13 +27,30 @@ def count_words(line):
def main(): def main():
# Better argument parsing # Better argument parsing
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-c', '--chapter', action='store_true') parser.add_argument(
parser.add_argument('-a', '--act', action='store_true') '-c',
parser.add_argument('-pp', action='store_true') '--chapter',
parser.add_argument('filename') action='store_true',
help='output chapter-by-chapter breakdown of word counts, including how many words in each chapter are tagged with which status',
)
parser.add_argument(
'-a',
'--act',
action='store_true',
help='output act-by-act breakdown of word counts (total only)',
)
parser.add_argument(
'-pp',
action='store_true',
help='run markdown pre-processor, this allows for a multi-file input (e.g. each chapter in its own file), but requires the MarkdownPP python library',
)
parser.add_argument(
'markdown_file',
type=argparse.FileType('r'),
help='The markdown file for the novel, main file if a multi-file novel',
)
arguments = parser.parse_args() arguments = parser.parse_args()
filename = arguments.filename
mdfile = None mdfile = None
if arguments.pp: if arguments.pp:
@ -43,10 +59,12 @@ def main():
import MarkdownPP import MarkdownPP
mdfile = tempfile.TemporaryFile(mode='w+') mdfile = tempfile.TemporaryFile(mode='w+')
MarkdownPP.MarkdownPP(input=open(filename), output=mdfile, modules=list(MarkdownPP.modules)) MarkdownPP.MarkdownPP(
input=arguments.markdown_file, output=mdfile, modules=list(MarkdownPP.modules)
)
mdfile.seek(0) mdfile.seek(0)
else: else:
mdfile = open(filename) mdfile = arguments.markdown_file
chapter_heading = None chapter_heading = None
act_heading = None act_heading = None