@classmethod
    def from_argparse(cls, parser, inputs=None, outputs=None, source=None):
        from StringIO import StringIO

        opts = cls(source=source)
        buf = StringIO()
        parser.print_usage(buf)
        opts._usage = buf.getvalue().strip()
        buf.close()

        buf = StringIO()
        parser.print_help(buf)
        opts._help = buf.getvalue().strip()
        buf.close()

        inputs = [re.sub(r'^-*', '', s) for s in inputs] if inputs else []
        outputs = [re.sub(r'^-*', '', s) for s in outputs] if outputs else []
        for action in parser._optionals._actions:
            long = None
            short = None
            const = None
            option_type = TYPE_OPTION
            if action.dest in inputs or \
               action.dest == 'input' or \
               (action.default and action.default == sys.stdin):
                option_type = TYPE_INPUT
            elif action.dest in outputs or \
                    action.dest == 'output' or \
                    (action.default and action.default == sys.stdout):
                option_type = TYPE_OUTPUT

            for s in action.option_strings:
                if s.startswith("--") and long is None:
                    long = s
                elif s.startswith("-") and short is None:
                    short = s
            if hasattr(action, "const"):
                const = action.const

            opts.add(Option(
                action.dest,
                long=long,
                type=action.type,
                option_type=option_type,
                short=short,
                const=const,
                nargs=action.nargs,
                required=action.required,
                default=action.default if action.dest != "help" else None,
            ))
        return opts
Create Options from a given argparse parser.
dataset/ETHPy150Open thasso/pyjip/jip/options.py/Options.from_argparse