From 937ff835b5808f184162d68d5eb93e12a7c5de86 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 25 Mar 2018 19:44:00 +0000 Subject: md_tw.py: Update main. * md_tw.py (main): Update function. --- md_tw.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/md_tw.py b/md_tw.py index 458e6c0..1ceb800 100644 --- a/md_tw.py +++ b/md_tw.py @@ -18,6 +18,7 @@ # along with markdown-textwrap (see COPYING). If not, see # . +import argparse import re import textwrap @@ -516,4 +517,34 @@ class TWMarkdown(mistune.Markdown): def main(): - print('USAGE: md_tw 72 file.md file2.md [...]') + def parse_args(): + parser = argparse.ArgumentParser() + + # Options for args. + w_opts = { + 'type': int, + 'dest':'width', + 'default':72, + 'help': 'Max. line width. Default is 72.' + } + f_opts = { + 'type': argparse.FileType('r'), + 'help': 'File path of Markdown document.' + } + + # Define expected args. + parser.add_argument('-w', '--width', **w_opts) + parser.add_argument('md_file', **f_opts) + + # Parse 'em. + a = parser.parse_args() + + return {'width': a.width, 'text': a.md_file.read()} + + def wrap(text, width): + return TWMarkdown(tw_width=width)(text) + + def out(text): + print('{}'.format(text)) + + return out(wrap(**parse_args())) -- cgit v1.2.3