python - How can I build sub-commands to bundle scripts that should also work independently? -



python - How can I build sub-commands to bundle scripts that should also work independently? -

i working on research project (my bachelors thesis) handwriting recognition. wrote lot of python scripts far , create them useful other people. created project on pypi: https://pypi.python.org/pypi/hwrt/

currently, 2 executable scripts there: backup.py , view.py. when installed via pip can phone call them, works:

$ backup.py --help usage: backup.py [-h] [-d folder] [-s] [-o] download raw info online server , (e.g. on dropbox) handwriting_datasets.pickle. optional arguments: -h, --help show help message , exit -d folder, --destination folder write handwriting_dataset.pickle (default: /home/moose/downloads/write-math/archive /raw-datasets) -s, --small should little dataset (with capital letters) created? (default: false) -o, --onlydropbox don't download new files; upload dropbox (default: false) $ view.py --help usage: view.py [-h] [-i id] [--mysql mysql] [-m folder] display raw_data_id. optional arguments: -h, --help show help message , exit -i id, --id id raw_data_id want? --mysql mysql mysql configuration should used? -m folder, --model folder model folder (with info.yml)?

i got via scripts in setup.py:

try: setuptools import setup except importerror: distutils.core import setup config = { 'name': 'hwrt', 'version': '0.1.19', 'author': 'martin thoma', 'author_email': 'info@martin-thoma.de', 'packages': ['hwrt'], 'scripts': ['bin/backup.py', 'bin/view.py'], 'url': 'https://github.com/martinthoma/hwrt', 'license': 'mit', 'description': 'handwriting recognition tools', 'long_description': """a tookit handwriting recognition. developed part of bachelors thesis of martin thoma.""", 'install_requires': [ "argparse", "theano", "nose", ], 'keywords': ['hwrt', 'recognition', 'handwriting', 'on-line'], 'download_url': 'https://github.com/martinthoma/hwrt', } setup(**config)

however, rather want them called this:

$ hwrt backup --help (just came before 'backup.py --help') $ hwrt view --help (just came before 'view.py --help') $ hwrt --help (a list of sub-commands)

i know can done sub-commands , argparse. however, mean had create new script bundle commands argparse. scripts work independently. feels more logically me adjust command line parameters of import backup.py in backup.py , not in file.

is there way adjust scripts "discover" scripts in bin folder , add together of them sub-commands?

this might case using using parents.

for example, lets assume both of scripts create parser object when loaded (or have function creates parser):

import argparse backup import parser backup_parser view import parser view_parser if __name__=='__main__': parser = argparse.argumentparser() subparsers = parser.add_subparsers(dest='cmd') # or utilize parser.setdefault() described in documentation backup = subparsers.add_parser('backup', add_help=false, parents=[backup_parser]) view = subparsers.add_parser('view', add_help=false, parents=[view_parser]) args = parser.parse_args()

this should print appropriate helps. args.cmd identify subcommand, , other attributes respective arguments. backup subparser clone of parser imported backup.py. (i haven't tested script there may typos or bugs, gives general idea.)

how handle cli subcommands argparse discusses couple of ways of handling subcommands.

ipython uses argparse handle both main interface, , many of magic commands. populates parsers arguments , values config files. way big number of parameters can set either default configs, customized configs, or on command line.

python setuptools argparse

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -