{"action":"created","issue":{"url":"https://api.github.com/repos/python-cmd2/cmd2/issues/596","repository_url":"https://api.github.com/repos/python-cmd2/cmd2","labels_url":"https://api.github.com/repos/python-cmd2/cmd2/issues/596/labels{/name}","comments_url":"https://api.github.com/repos/python-cmd2/cmd2/issues/596/comments","events_url":"https://api.github.com/repos/python-cmd2/cmd2/issues/596/events","html_url":"https://github.com/python-cmd2/cmd2/issues/596","id":376951845,"node_id":"MDU6SXNzdWUzNzY5NTE4NDU=","number":596,"title":"Pass a custom Namespace to args","user":{"login":"teto","id":886074,"node_id":"MDQ6VXNlcjg4NjA3NA==","avatar_url":"https://avatars0.githubusercontent.com/u/886074?v=4","gravatar_id":"","url":"https://api.github.com/users/teto","html_url":"https://github.com/teto","followers_url":"https://api.github.com/users/teto/followers","following_url":"https://api.github.com/users/teto/following{/other_user}","gists_url":"https://api.github.com/users/teto/gists{/gist_id}","starred_url":"https://api.github.com/users/teto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/teto/subscriptions","organizations_url":"https://api.github.com/users/teto/orgs","repos_url":"https://api.github.com/users/teto/repos","events_url":"https://api.github.com/users/teto/events{/privacy}","received_events_url":"https://api.github.com/users/teto/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-11-02T20:10:44Z","updated_at":"2018-11-03T14:33:05Z","closed_at":null,"author_association":"NONE","body":"Kind of a follow up on https://github.com/python-cmd2/cmd2/issues/498\r\n\r\nI want to pass a custom Namespace when parsing arguments while keeping the autocompletion:\r\n\r\n```\r\n    @with_argparser(parser)\r\n    def do_load_pcap(self, cmdline):\r\n          myNamespace = Namespace()\r\n          myNamespace.toto = self.data\r\n          args = parser.parse_args(shlex.split(cmdline), myNamespace)\r\n          \r\n```\r\nI modified \r\nhttps://github.com/python-cmd2/cmd2/blob/b6c39365e0331ab64aad074565fe73fb074a3a7f/cmd2/cmd2.py#L270-L271\r\nso that it doesn't parse stuff.\r\n```\r\ndef with_argparser_test(argparser: argparse.ArgumentParser,\r\n                   preserve_quotes: bool=False) -> Callable[[argparse.Namespace], Optional[bool]]:\r\n    \"\"\"A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments\r\n    with the given instance of argparse.ArgumentParser.\r\n\r\n    :param argparser: unique instance of ArgumentParser\r\n    :param preserve_quotes: if True, then arguments passed to argparse maintain their quotes\r\n    :return: function that gets passed the argparse-parsed args\r\n    \"\"\"\r\n    import functools\r\n\r\n    # noinspection PyProtectedMember\r\n    def arg_decorator(func: Callable[[Any], Optional[bool]]):\r\n        @functools.wraps(func)\r\n        def cmd_wrapper(instance, cmdline):\r\n            # lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)\r\n            # try:\r\n            #     args = argparser.parse_args(lexed_arglist)\r\n            # except SystemExit:\r\n            #     return\r\n            # else:\r\n            return func(instance, cmdline)\r\n\r\n        # argparser defaults the program name to sys.argv[0]\r\n        # we want it to be the name of our command\r\n        # argparser.prog = func.__name__[len(COMMAND_FUNC_PREFIX):]\r\n\r\n        # If the description has not been set, then use the method docstring if one exists\r\n        if argparser.description is None and func.__doc__:\r\n            argparser.description = func.__doc__\r\n\r\n        # Set the command's help text as argparser.description (which can be None)\r\n        # cmd_wrapper.__doc__ = argparser.description\r\n\r\n        # Mark this function as having an argparse ArgumentParser\r\n        setattr(cmd_wrapper, 'argparser', argparser)\r\n\r\n        return cmd_wrapper\r\n\r\n    return arg_decorator\r\n\r\n```\r\nwhich can be used like\r\n```\r\n    @with_argparser_test(parser)\r\n    def do_load_pcap(self, args):\r\n        \"\"\"\r\n        Load the file as the current one\r\n        \"\"\"\r\n        args = shlex.split(args)\r\n        parser = self.do_load_pcap.argparser\r\n        args = parser.parse_args(args)\r\n        \r\n        self.poutput(\"Loading %s\" % args.input_file)\r\n\r\n```\r\n\r\nI wonder if cmd2 could propose some better interface. This is just an example, that adds a parameter \"dont_parse\" to the with_argparser decorator. If it's true, cmd2 could pass the parser along to the member function (instead of `parser = self.do_load_pcap.argparser` which requires some knowledge about cmd2 implementation)\r\n```\r\n@with_argparser(dont_quote, dont_parse=True)\r\ndef func(self, parser, args):\r\n      parser.parse_args(args)\r\n```"},"comment":{"url":"https://api.github.com/repos/python-cmd2/cmd2/issues/comments/435592440","html_url":"https://github.com/python-cmd2/cmd2/issues/596#issuecomment-435592440","issue_url":"https://api.github.com/repos/python-cmd2/cmd2/issues/596","id":435592440,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNTU5MjQ0MA==","user":{"login":"tleonhardt","id":2812720,"node_id":"MDQ6VXNlcjI4MTI3MjA=","avatar_url":"https://avatars1.githubusercontent.com/u/2812720?v=4","gravatar_id":"","url":"https://api.github.com/users/tleonhardt","html_url":"https://github.com/tleonhardt","followers_url":"https://api.github.com/users/tleonhardt/followers","following_url":"https://api.github.com/users/tleonhardt/following{/other_user}","gists_url":"https://api.github.com/users/tleonhardt/gists{/gist_id}","starred_url":"https://api.github.com/users/tleonhardt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tleonhardt/subscriptions","organizations_url":"https://api.github.com/users/tleonhardt/orgs","repos_url":"https://api.github.com/users/tleonhardt/repos","events_url":"https://api.github.com/users/tleonhardt/events{/privacy}","received_events_url":"https://api.github.com/users/tleonhardt/received_events","type":"User","site_admin":false},"created_at":"2018-11-03T14:33:05Z","updated_at":"2018-11-03T14:33:05Z","author_association":"MEMBER","body":"@teto I don't understand your motivation.  What capability do you want that our current ``argparse`` decorators don't support?  If you are going to end up parsing the command line arguments with ``argparse`` anyways inside your command method, why not just let one of our decorators do it for you?\r\n\r\nIf you can explain your use case and it is something that would also plausibly make sense for other users, then I would be happy to create an additional decorator that accepts an ``argparse.ArgumentParser`` and uses it for tab-completion purposes but doesn't parse the command line args and simply passes both the command line arguments and the parser to the method.  The hardest part of that would likely be coming up with a good name for the new decorator.  I would strongly prefer to not combine this functionality with the existing decorator via an optional argument as I think that could lead to confusion for users.\r\n\r\nBut for starters, please explain your motivation further and why you can't accomplish what you want with ``cmd2`` as it exists now ...\r\n\r\n"}}
{"actor":{"display_login":"tleonhardt"}}