How to use the docopt.formal_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 boutiques / boutiques / tools / python / boutiques / util / parserHelper.py View on Github external
def docoptToArgumentParser(self, docopt_str):
        # initial doc validation
        extc_dict = dcpt.docopt(docopt_str)

        usage_sections = dcpt.parse_section('usage:', docopt_str)

        options = dcpt.parse_defaults(docopt_str)
        pattern = dcpt.parse_pattern(
            dcpt.formal_usage(usage_sections[0]), options)
        argv = dcpt.parse_argv(dcpt.Tokens(sys.argv[1:]), list(options), False)
        pattern_options = set(pattern.flat(dcpt.Option))
        for options_shortcut in pattern.flat(dcpt.OptionsShortcut):
            doc_options = dcpt.parse_defaults(docopt_str)
            options_shortcut.children = list(set(doc_options) - pattern_options)
        matched, left, collected = pattern.fix().match(argv)

        # can loop through to compare extracted params with extc_dict
        # and add param to argparser for each prm
        for prm in pattern.flat() + collected:
            print(prm)
github boutiques / boutiques / tools / python / boutiques / importer.py View on Github external
del self.descriptor['groups']
        del self.descriptor['inputs']
        del self.descriptor['output-files']
        self.docopt_str = docopt_str
        self.dependencies = collections.OrderedDict()
        self.all_desc_and_type = collections.OrderedDict()
        self.unique_ids = []

        try:
            # docopt code snippet to extract args tree (pattern)
            # should run if docopt script is valid
            options = parse_defaults(docopt_str)

            self.pattern = parse_pattern(
                formal_usage(self._parse_section('usage:', docopt_str)[0]),
                options)

            argv = parse_argv(
                TokenStream(sys.argv[1:], DocoptLanguageError),
                list(options), False)
            pattern_options = set(self.pattern.flat(Option))

            for options_shortcut in self.pattern.flat(AnyOptions):
                doc_options = parse_defaults(docopt_str)
                options_shortcut.children = list(
                    set(doc_options) - pattern_options)
            matched, left, collected = self.pattern.fix().match(argv)
        except Exception:
            os.remove(base_descriptor)
            raise_error(ImportError, "Invalid docopt script")