commit 7d34e81bdb8ca395ca2fc06cd313e68229b2ff30
parent 0c532061664e790846e90a61c2814831f60dc2a1
Author: oscarbenedito <oscar@obenedito.org>
Date:   Sat, 16 Nov 2019 19:43:22 +0100

Added automatic output directory option

Diffstat:
MREADME.md | 14++++++++++++--
Mmarkion.py | 3+++
2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md @@ -43,10 +43,19 @@ One of the arguments is the name of the input file: ```python file markion.py parser.add_argument('file', metavar='file', type=str, nargs=1, help='Input file.') ``` -The other argument is optional and it let's the user specify the output directory. +Another optional argument lets the user specify the output directory. ```python file markion.py parser.add_argument('-d', '--output-directory', dest='out_dir', type=str, default=os.getcwd(), help='Change the output directory.') ``` +The other argument is also optional and it lets the program automatically detect the output directory (based on the file's directory). This option will override the `--output-directory` option. +```python file markion.py +parser.add_argument('-D', '--auto-directory', dest='auto_dir', action='store_true', help='Auto detect output directory.') +``` +To calculate the directory automatically, we simply check the input file's directory. +```python block auto_dir +if args.auto_dir: + args.out_dir = os.path.dirname(args.file[0]) +``` Finally we assign the arguments' values to the `args` variable to use it later on. ```python file markion.py args = parser.parse_args() @@ -98,8 +107,9 @@ def resolve(content, blocks): ``` ### Writing the output to the corresponding files -Finally, if there weren't any errors, we write the output code into the respective files. To do so we create the output directory if not already created: +Finally, if there weren't any errors, we write the output code into the respective files. To do so, we assign the directory automatically if the option has been delcared, otherwise, we create the output directory if not already created: ```python file markion.py +[[ include auto_dir ]] if not os.path.exists(args.out_dir): os.mkdirs(args.out_dir) ``` diff --git a/markion.py b/markion.py @@ -19,6 +19,7 @@ import os, sys, re, argparse parser = argparse.ArgumentParser(description='Markion is a simple scripts that retrieves tangled code from Markdown.') parser.add_argument('file', metavar='file', type=str, nargs=1, help='Input file.') parser.add_argument('-d', '--output-directory', dest='out_dir', type=str, default=os.getcwd(), help='Change the output directory.') +parser.add_argument('-D', '--auto-directory', dest='auto_dir', action='store_true', help='Auto detect output directory.') args = parser.parse_args() with open(args.file[0], 'r') as f: inp = f.read() @@ -46,6 +47,8 @@ for f in files: if f[0] not in file_content: file_content[f[0]] = '' file_content[f[0]] += resolve(f[1], block_content) +if args.auto_dir: + args.out_dir = os.path.dirname(args.file[0]) if not os.path.exists(args.out_dir): os.mkdirs(args.out_dir) for fn, fc in file_content.items():