How to use the docopt.printable_usage function in docopt

To help you get started, we’ve selected a few docopt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Danamir / dyn-gandi / dyn_gandi.py View on Github external
def cli():
    """Command-line interface"""

    global options
    options = docopt(__doc__)
    try:
        main()
    except RuntimeWarning as w:
        print("  Warning: %s" % w, file=sys.stderr)
        sys.exit(1)
    except RuntimeError as w:
        print("%s" % w, file=sys.stderr)
        print(docpt.printable_usage(__doc__))
        sys.exit(1)
github instantshare / instantshare / src / cli / main.py View on Github external
def _get_module(cmd):
    if cmd is None:
        print(printable_usage(__doc__), file=sys.stderr)
        sys.exit(1)
    elif cmd not in __commands__:
        print("Command not found. Available commands: " + ", ".join(__commands__), file=sys.stderr)
        sys.exit(1)
    else:
        return import_module(__package__ + "." + cmd)
github IDSIA / sacred / sacred / experiment.py View on Github external
def get_usage(self, program_name=None):
        """Get the commandline usage string for this experiment."""
        program_name = os.path.relpath(
            program_name or sys.argv[0] or "Dummy", self.base_dir
        )
        commands = OrderedDict(self.gather_commands())
        long_usage = format_usage(
            program_name, self.doc, commands, self.all_cli_options
        )
        # internal usage is a workaround because docopt cannot handle spaces
        # in program names. So for parsing we use 'dummy' as the program name.
        # for printing help etc. we want to use the actual program name.
        internal_usage = format_usage("dummy", self.doc, commands, self.all_cli_options)
        short_usage = printable_usage(long_usage)
        return short_usage, long_usage, internal_usage
github isbg / isbg / isbg / sa_unwrap.py View on Github external
def isbg_sa_unwrap():
    """Run when this module is called from the command line."""
    try:
        opts = docopt(__isbg_sa_unwrap_opts__.__doc__,
                      version="isbg_sa_unwrap v" + isbg.__version__ +
                      ", from: " + os.path.abspath(__file__) + "\n\n" +
                      isbg.__license__)
    except DocoptExit:
        sys.stderr.write('Error with options!!!\n')
        raise

    if opts.get("--usage"):
        sys.stdout.write(
            "{}\n".format(printable_usage(__isbg_sa_unwrap_opts__.__doc__)))
        return

    if opts.get("--from"):
        file_in = open(opts["--from"], 'rb')
    else:
        file_in = sys.stdin

    if hasattr(file_in, 'buffer'):
        file_in = file_in.buffer    # pylint: disable=no-member

    spams = unwrap(file_in.read())
    file_in.close()

    if spams is None:
        sys.stderr.write("No spam into the mail detected.\n")
        return
github isbg / isbg / isbg / __main__.py View on Github external
>>> sbg = isbg.ISBG()
        >>> parse_args(sbg)
    """
    try:
        opts = docopt(__cmd_opts__.__doc__, version="isbg_v" +
                      isbg.__version__ + ", from: " +
                      os.path.abspath(__file__) + "\n\n" + isbg.__license__)
        opts = dict([(k, v) for k, v in opts.items()
                     if v is not None])
    except DocoptExit as exc:
        raise isbg.ISBGError(isbg.__exitcodes__['flags'],
                             "Option processing failed - " + str(exc))

    if opts.get("--usage"):
        print(printable_usage(__cmd_opts__.__doc__))
        return 1

    if opts.get("--deletehigherthan") is not None:
        try:
            sbg.deletehigherthan = float(opts["--deletehigherthan"])
        except Exception:  # pylint: disable=broad-except
            raise isbg.ISBGError(isbg.__exitcodes__['flags'],
                                 "Unrecognized score - " +
                                 opts["--deletehigherthan"])
        if sbg.deletehigherthan < 1:
            raise isbg.ISBGError(isbg.__exitcodes__['flags'],
                                 "Score " + repr(sbg.deletehigherthan) +
                                 " is too small")

    if opts["--flag"] is True:
        sbg.spamflags.append("\\Flagged")