def get_args(): """ Parse argv """ import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) action = parser.add_mutually_exclusive_group(required=True) action.add_argument( "--random", action="store_true", help="Choose random next background and move to top of stack") action.add_argument("--random-force", action="store_true", help="Cycle irrespective of cycling setting") action.add_argument("--restore", action="store_true", help="Restore the current background") action.add_argument("--toggle", action="store_true", help="Toggle cycling") action.add_argument("--next", action="store_true", help="Move to next background in stack") action.add_argument("--prev", action="store_true", help="Move to previous background in stack") action.add_argument("--init", action="store_true", help="Initialise the background stack") return parser.parse_args()
def get_args(): """ Parse argv """ parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("N", type=int, nargs="?", default=10 ** 6, help="Number of samples to generate") return parser.parse_args()
def get_args(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "input", type=argparse.FileType("r"), default="-", nargs="?", help="Input file to be prettified") parser.add_argument( "output", type=argparse.FileType("w"), default="-", nargs="?", help="Output file") return parser.parse_args()
def get_args(): """ Parse argv """ import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("n", type=int, nargs="?", default=8, help="Board size") return parser.parse_args()
def get_args(): """ Parse argv """ import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("-w", type=int, help="width of table") parser.add_argument("-l", type=int, help="height of table") return parser.parse_args()
def get_args(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("n", nargs="?", type=int, default=100, help="Number of squares to check") parser.add_argument("--max", type=int, default=1000, help="Largest exponent of 10 to check") return parser.parse_args()
def get_args(): """ Parse command line arguments """ import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("from_base", type=int, help="Base of the string in stdin") parser.add_argument("to_base", nargs="?", type=int, default=10, help="Base of the string to output to stdout") return parser.parse_args()
def get_args(): """ Parse argv """ import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("y", type=float, default=1.0, help="Spec to intersect to_spec with") args = parser.parse_args() if args.y < 1.0 or args.y > mpmath.factorial(100): parser.error("y should be >= 1 and <= 100!") return args
def get_args(): """ Parse argv """ parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--prefix", default="arg_", help="Prefix to attach to the outputted shell variables") parser.add_argument("name", help="Program name to display") parser.add_argument("arguments", nargs="*", default=[], help="Arguments to be passed on to the parser") return parser.parse_args()
def caller_argparse(mod_code, argv, prog): """ Apply a caller's argument parsing to the caller's arguments """ mod_code_pre, *mod_code_post_list = re.split("\n-{3,}\n", mod_code) # Add our own help so we can use a non-zero exit code parser = argparse.ArgumentParser(prog=prog, add_help=False) parser.add_argument("-h", "--help", action=ErrorHelpAction, help="show this help message and exit") exec(mod_code_pre) args = parser.parse_args(argv) for mod_code_post in mod_code_post_list: exec(mod_code_post) return args
def get_args(): """ Parse argv """ import smartparse as argparse parser = argparse.ArgumentParser() parser.add_argument("n", type=int, nargs="?", default=5, help="Order of bracketing to produce") parser.add_argument("operator", nargs="?", default=DEFAULT_OPERATOR, help="Operator to format bracketings with. Can be empty.") calc_action = parser.add_mutually_exclusive_group() calc_action.add_argument("--predict", action="store_true", help="Only calculate the number, do not show them all.") calc_action.add_argument("--dp-predict", action="store_true", help="Only calculate the number with DP, do not show them all.") return parser.parse_args()
def get_args(): """ Parse argv """ import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("mean", type=float, nargs="?", default=-1.0, help="""The mean of the normal distribution of the number of diacritics added for each character""") parser.add_argument("sd", type=float, nargs="?", default=5.0, help="""The standard deviation of the normal distribution of the number of diacritics added for each character""") parser.add_argument("-r", "--revert", action="store_true", help="Strip diacritical marks instead of adding them") parser.add_argument("-u", "--unbuffered", action="store_true", help="Reconfigure I/O objects to use line buffering") return parser.parse_args()
def get_args(): """ Process argv """ import smartparse as argparse config = get_config() parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("file", type=argparse.FileType("r"), help="File to process") parser.add_argument("--font", type=str, default=config["font"], help="Figlet font to use") parser.add_argument("--dir", type=dir_arg, default=pathlib.Path(config["dir"]).expanduser(), help="Font directory to use") parser.add_argument("--flags", type=str, default=config["flags"], help="Figlet flags to use") parser.add_argument( "--exec", type=str, nargs="+", help="Alternative command to run - overrides any figlet config") parser.add_argument("--nobackup", dest="backup", action="store_false", help="Don't make backups (not recommended)") parser.add_argument( "--nooverwrite", dest="overwrite", action="store_false", help=("Don't overwrite the file, instead write the modified file to" " stdout. For testing purposes mostly.")) return parser.parse_args()
def get_args(): """ Parse argv """ import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) file_group = parser.add_mutually_exclusive_group() # important that this argument goes first so the default gets triggered file_group.add_argument("-f", "--file", type=argparse.FileType("r"), default=str(LIPSUM_DIR / "lipsum.txt"), help="File to read paragraphs from, spaced with empty lines") file_group.add_argument("-s", "--shakespeare", action="store_const", const=(LIPSUM_DIR / "shakespeare.txt").open("r"), dest="file", help="Print lines from the complete works of Shakespeare") file_group.add_argument("-p", "--pasta", type=PastaHashable, choices=PastaHashable.REGISTRY, dest="file", help="Use a copypasta") parser.add_argument("paragraphs", type=int, nargs="?", default=5, help="""Number of paragraphs to generate. A negative integer `-n` is taken to mean "n copies of the whole file".""") return parser.parse_args()
def get_args(): """ Parse argv """ import smartparse as argparse class ListAction(argparse._HelpAction): """ Action that must override any other parsing to produce a list and exit, which is actually very similar functionality to -h, so we reappropriate HelpAction. """ def __call__(self, parser, namespace, values, option_string=None): print("Base alphabets:") summarise_alphabets(alphabets) print("Composed alphabets:") summarise_alphabets(auxiliary) sys.exit() parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "-l", "--list", action=ListAction, help="""Do not translate, instead produce a list of available alphabets.""") parser.add_argument( "-d", "--dryrun", action="store_true", help="Simply display the translation rules once processed") parser.add_argument("from_spec", help="Spec for the set of characters to be translated") parser.add_argument( "to_spec", help="Spec for the set of characters to be translated to") parser.add_argument("from_spec_sub", nargs="?", default="", help="Spec to subtract from from_spec") parser.add_argument("to_spec_sub", nargs="?", default="", help="Spec to subtract from to_spec") parser.add_argument("from_spec_inter", nargs="?", default="", help="Spec to intersect from_spec with") parser.add_argument("to_spec_inter", nargs="?", default="", help="Spec to intersect to_spec with") parser.add_argument( "-u", "--unbuffered", action="store_true", help="""Flush stdout after each line, Useful in pipes if you want immediate output""") args = parser.parse_args() if not args.to_spec or not args.from_spec: parser.error("from_spec and to_spec should be nonempty.") return args
Optionally, you can leave it as None and pass in `unicode` as truthy or falsey to get default values for ascii or unicode-supporting terminals. """ if prog_chars is None: if unicode is not None: prog_chars = UNICODE_PROG_CHARS if unicode else ASCII_PROG_CHARS else: raise ValueError("Need either to pass unicode, or pass characters") progression, block, fillchar = prog_chars integral, fractional = divmod(width * fraction, 1) return "{}{}".format(block * int(integral), progression[int(len(progression) * fractional)] if fractional else "").ljust(width, fillchar) if __name__ == "__main__": import sys import time import smartparse as argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("-a", "--ascii", action="store_true", help="ASCII mode") args = parser.parse_args() TOTAL = 1000 for i in range(TOTAL + 1): time.sleep(0.01) print("\r[{}]".format(format_bar(78, i / TOTAL, unicode=not args.ascii)), end="", file=sys.stderr, flush=True) print(file=sys.stderr)