Last active
March 16, 2016 23:12
-
-
Save JoshuaEnglish/c0fa6e626412d7eb3ab8 to your computer and use it in GitHub Desktop.
Template for integrating argparse.ArgumentParsers from plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Wed Mar 16 14:04:54 2016 | |
@author: jenglish | |
""" | |
import argparse | |
parser = argparse.ArgumentParser('t', description="Todo Manager") | |
commands = parser.add_subparsers(title='commands', | |
dest="command", | |
help="supported commands", | |
) | |
parser.add_argument('-i', '--interactive', '-l', '--loop', dest="interact", | |
action='store_true', default=False) | |
list_parser = commands.add_parser('list', help='list tasks', | |
description="Tool for listing tasks", | |
) | |
list_parser.add_argument('-n', action="store_false", | |
dest="by_pri", default=True, | |
help="Prints task list in numerical order, otherwise orders by priority") | |
list_parser.add_argument('-f', dest="filters", nargs="*", | |
help="Only lists tasks containing these words") | |
list_parser.add_argument('-y', dest="filterop", action="store_true", | |
default=False, | |
help="Shows tasks matching any filter word. Default is to match all") | |
list_parser.add_argument('-a', dest="showcomplete", action="store_true", default=False, | |
help="Show completed (but not archived) tasks.") | |
list_parser.add_argument('-x', dest="showext", action="store_true", default=False, | |
help="Shows hidden text extensions") | |
add_parser =commands.add_parser('add', help="add a task") | |
add_parser.add_argument(nargs="+", dest="text", | |
help="text of the new task") | |
do_parser = commands.add_parser('do', help="mark a task as complete") | |
do_parser.add_argument('tasknum', type=int, | |
help="number of the task to complete") | |
do_parser.add_argument('comment', nargs=argparse.REMAINDER, | |
help="comment to add to the completed task") | |
# assume report_parser is defined elsewhere and imported | |
report_parser = argparse.ArgumentParser('report') | |
report_parser.add_argument('text', nargs=argparse.REMAINDER) | |
# commands has a dictionary it uses to store the subcommands | |
name = report_parser.prog # assume no prefix commands have been put in | |
# update report_parser.prog | |
report_parser.prog = "%s %s" % (parser.prog, name) | |
commands.choices[name] = report_parser | |
# to include this in help we need a separate report_help string | |
choice_action = commands._ChoicesPseudoAction(name, (), "report on tasks") | |
commands._choices_actions.append(choice_action) | |
def t(stuff): | |
args = parser.parse_args(stuff.split()) | |
return args | |
if __name__=='__main__': | |
import sys | |
args = parser.parse_args(sys.argv[1:]) | |
print(args) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment