summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2018-03-25 19:44:00 +0000
committerrsiddharth <s@ricketyspace.net>2018-03-25 19:44:00 +0000
commit937ff835b5808f184162d68d5eb93e12a7c5de86 (patch)
tree8f2640f53130eab687a0f9a672e4b70144611d70
parente357ce168c8f6a78df349fbb262d71bf46fe11b5 (diff)
md_tw.py: Update main.
* md_tw.py (main): Update function.
-rw-r--r--md_tw.py33
1 files changed, 32 insertions, 1 deletions
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
# <http://www.gnu.org/licenses/>.
+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()))