From 5df3a48f35ba39d0a8f4f740e6b8eb457be7bd04 Mon Sep 17 00:00:00 2001 From: Dmytro Yeroshkin Date: Sat, 23 Oct 2021 14:08:12 +0200 Subject: [PATCH] Cleaned up argument parsing --- novel_stats/novel_stats.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/novel_stats/novel_stats.py b/novel_stats/novel_stats.py index 0d8395b..5181d06 100755 --- a/novel_stats/novel_stats.py +++ b/novel_stats/novel_stats.py @@ -1,10 +1,9 @@ #!/usr/bin/python -import collections -import sys -import tempfile import argparse +import collections +import tempfile CHAPTER_MARKER = '## ' STATUS_MARKER = '[status]: # ' @@ -28,13 +27,30 @@ def count_words(line): def main(): # Better argument parsing parser = argparse.ArgumentParser() - parser.add_argument('-c', '--chapter', action='store_true') - parser.add_argument('-a', '--act', action='store_true') - parser.add_argument('-pp', action='store_true') - parser.add_argument('filename') + parser.add_argument( + '-c', + '--chapter', + 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() - filename = arguments.filename mdfile = None if arguments.pp: @@ -43,10 +59,12 @@ def main(): import MarkdownPP 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) else: - mdfile = open(filename) + mdfile = arguments.markdown_file chapter_heading = None act_heading = None