Argument parsing #2
|
@ -4,6 +4,7 @@
|
||||||
import collections
|
import collections
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import argparse
|
||||||
|
|
||||||
CHAPTER_MARKER = '## '
|
CHAPTER_MARKER = '## '
|
||||||
STATUS_MARKER = '[status]: # '
|
STATUS_MARKER = '[status]: # '
|
||||||
|
@ -25,11 +26,18 @@ def count_words(line):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
arguments = sys.argv[1:]
|
# Better argument parsing
|
||||||
filename = arguments[0]
|
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')
|
||||||
|
arguments = parser.parse_args()
|
||||||
|
|
||||||
|
filename = arguments.filename
|
||||||
mdfile = None
|
mdfile = None
|
||||||
|
|
||||||
if '-pp' in arguments:
|
if arguments.pp:
|
||||||
# -pp flag to allow Markdown Preprocessing primarily to allow multi-file novel formatting
|
# -pp flag to allow Markdown Preprocessing primarily to allow multi-file novel formatting
|
||||||
# this is implemented using a temporary file created using python's buit-in tempfile library
|
# this is implemented using a temporary file created using python's buit-in tempfile library
|
||||||
import MarkdownPP
|
import MarkdownPP
|
||||||
|
@ -88,7 +96,7 @@ def main():
|
||||||
total_word_count += word_count_by_chapter[chapter_heading]
|
total_word_count += word_count_by_chapter[chapter_heading]
|
||||||
|
|
||||||
# -c or --chapter to give a chapter-by-chapter word count summary.
|
# -c or --chapter to give a chapter-by-chapter word count summary.
|
||||||
if '-c' in arguments or '--chapter' in arguments:
|
if arguments.chapter:
|
||||||
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
for chapter_heading, chapter_word_count in word_count_by_chapter.items():
|
||||||
if chapter_heading is None:
|
if chapter_heading is None:
|
||||||
continue
|
continue
|
||||||
|
@ -108,15 +116,13 @@ def main():
|
||||||
print()
|
print()
|
||||||
|
|
||||||
# -a or --act to give an act-by-act word count summary.
|
# -a or --act to give an act-by-act word count summary.
|
||||||
if '-a' in arguments or '--act' in arguments:
|
if arguments.act:
|
||||||
for act_heading, act_word_count in word_count_by_act.items():
|
for act_heading, act_word_count in word_count_by_act.items():
|
||||||
if act_heading is None:
|
if act_heading is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print(
|
print(
|
||||||
'act {}: {:,} words (~{}%)'.format(
|
f'act {act_heading}: {act_word_count:,} words (~{act_word_count * 100// total_word_count}%)'
|
||||||
act_heading, act_word_count, act_word_count * 100 // total_word_count
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
print()
|
print()
|
||||||
|
|
Loading…
Reference in New Issue