python-cmd2 / cmd2

cmd2 - quickly build feature-rich and user-friendly interactive command line applications in Python
https://cmd2.readthedocs.io/en/stable/
MIT License
612 stars 114 forks source link

new behavior of path_complete #625

Closed teto closed 5 years ago

teto commented 5 years ago

the signature for path_complete changed in 0.9.7 path_filter: Optional[Callable[[str], bool]] = None) -> List[str]:

I tried to update my code but I have lost the autocompletion, everytime I press TAB, I just get the help:

+Ready>load_pcap ~/testbed/out/cli
Hint:
  INPUT_FILE              Either a pcap or a csv file.When a pcap is passed, mptcpanalyzer looks for a cached csvelse it generates a csv from the pcap with the external tshark program.

I used to add filename autocompletion via


                load_pcap = parser.add_argument(name, action=pcapAction, type=str, help='Pcap file')
                setattr(load_pcap, argparse_completer.ACTION_ARG_CHOICES, ('path_complete', [False, False]))

I tried to change it to setattr(load_pcap, argparse_completer.ACTION_ARG_CHOICES, ('path_complete', )) or setattr(load_pcap, argparse_completer.ACTION_ARG_CHOICES, ('path_complete', lambda x=True)) without success. What 's the correct upgrade ?

tleonhardt commented 5 years ago

We have always used it like the first way you tried to do it, except we combine the add_argument call inside the setattr. Here is an example from within cmd2 itself where we are setting up path completion for a positional argument to the create subcommand of the alias command:

setattr(alias_create_parser.add_argument('command_args', nargs=argparse.REMAINDER,
                                             help='arguments to pass to command'),
            ACTION_ARG_CHOICES, ('path_complete',))
tleonhardt commented 5 years ago

@kmvanbrunt Now that I think about it, can you take a look at how we use path_complete in the tab_autocompletion.py and tab_autocomp_dynamic.py examples, I think we forgot to update those examples when the signature changed.

kmvanbrunt commented 5 years ago

@tleonhardt I fixed the examples.

tleonhardt commented 5 years ago

@teto Have you been able to resolve your issue?

teto commented 5 years ago

not sure what happened since I changed nothing since Friday but the autocompletion seems to work again :) thanks