Skip to content

Instantly share code, notes, and snippets.

@William-Lake
Last active October 27, 2019 13:29
Show Gist options
  • Save William-Lake/d063781eb31575a532f0c8c5497ae112 to your computer and use it in GitHub Desktop.
Save William-Lake/d063781eb31575a532f0c8c5497ae112 to your computer and use it in GitHub Desktop.
Builds a __main__.py file gathering args via argparse. Covers a *very limited* range of options, primarily the ones I find useful. arg_parser_template.txt needs to be in the same dir as arg_parser_builder.py. Produces a __main__.py file in the same dir when finished.
def gather_input(prompt,empty_ok=False):
user_input = None
while user_input is None:
print(prompt)
user_input = input('?')
if not user_input.strip():
user_input = None
if user_input is not None or empty_ok:
break
return user_input
def multi_gather_input(prompt):
inputs = []
user_input = gather_input(prompt,empty_ok=True)
while user_input is not None:
inputs.append(user_input)
user_input = gather_input(prompt,empty_ok=True)
return inputs
def boolean_input(prompt):
user_input = gather_input(prompt).strip().upper()[0]
while user_input not in ['N','Y']:
print('Please provide valid input.')
user_input = gather_input(prompt).strip().upper()[0]
return user_input == 'Y'
if __name__ == "__main__":
# App Details
app_name = gather_input('App Name?')
app_description = gather_input('App Description?')
# Arg details
arg_names = multi_gather_input('Arg name?')
arg_details = {}
for arg_name in arg_names:
arg_details[arg_name] = {}
# --optional
if boolean_input(f'Is {arg_name} optional?'):
if not arg_name.startswith('--'):
del arg_details[arg_name]
arg_name = '--' + arg_name
arg_details[arg_name] = {}
arg_details[arg_name]['action'] = '\'store_true\''
elif boolean_input(f'Is {arg_name} a list?'):
if boolean_input('Is the list optional?'):
arg_details[arg_name]['nargs'] = '\'*\''
else:
arg_details[arg_name]['nargs'] = '\'+\''
elif boolean_input(f'Should {arg_name} be selected from a list of options?'):
choices = multi_gather_input(f'Please provide an option for {arg_name}.')
arg_details[arg_name]['choices'] = choices.__str__()
arg_details[arg_name]['help'] = "'" + gather_input(f'What\'s the description for {arg_name}?') + "'"
parser_str = f'arg_parser = ArgumentParser(\'{app_name}\',description=\'{app_description}\')'
arguments = []
for arg_name, ads in arg_details.items():
arg_str = ',\n\t\t'.join([
f'{name}={value}'
for name, value
in ads.items()
])
arg_str = f'''
arg_parser.add_argument(
\'{arg_name}\',
{arg_str}
)
'''
arguments.append(arg_str)
return_str = 'return ' + ', '.join([
f'args.{arg_name.replace("--","")}'
for arg_name
in arg_details.keys()
])
gather_str = ', '.join([
f'{arg_name.replace("--","")}'
for arg_name
in arg_details.keys()
]) + ' = gather_args()'
template = (
open('arg_parser_template.txt','r')
.read()
.replace('!PARSER!',parser_str)
.replace('!ARGUMENTS!','\n'.join(arguments))
.replace('!RETURN!',return_str)
.replace('!GATHER!',gather_str))
with open('__main__.py','w+') as out_file:
out_file.write(template)
import logging
from argparse import ArgumentParser
def gather_args():
!PARSER!
!ARGUMENTS!
args = arg_parser.parse_args()
!RETURN!
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
!GATHER!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment