gazebosim / gz-tools

Command line tools for the Gazebo libraries.
https://gazebosim.org
Apache License 2.0
15 stars 18 forks source link

Add bash completion #1

Closed osrf-migration closed 1 year ago

osrf-migration commented 8 years ago

Original report (archived issue) by Louise Poubel (Bitbucket: chapulina, GitHub: chapulina).


Pressing tab twice should complete with possible commands. Take the gz tool for example, pressing tab twice displays options such as camera, topic, etc.

scpeters commented 2 years ago

To start investigating this, I looked briefly at ros2cli, since it does a very good job of tab-completing in bash. It uses argcomplete, which is based in python and automatically works for packages that express their arguments using argparse. The following files is all that is needed to enable bash-completion:

That command is sourced as part of the general ros2 setup script, I believe because of the following configuration file:

If only we could just use argcomplete for ign! Unfortunately it does not currently use python. ign uses Ruby to look for *.yaml files in the IGN_CONFIG_PATH and parses these YAML files to determine the list of commands that can be executed. These command are implemented as Ruby files that either dlopen a c++ library to call a function or pass the CLI arguments to a C++ executable.

I think a first step would be to implement autocomplete for just the names of commands available in IGN_CONFIG_PATH. Once that is working, we can figure out how support autocomplete for the arguments to each command.

An additional reference is the gz completion script used in gazebo classic.

scpeters commented 2 years ago

or pass the CLI arguments to a C++ executable.

Our executables are currently using a fork of cli11 for argument parsing, which has an open feature request to support autocompletion: https://github.com/CLIUtils/CLI11/issues/343.

scpeters commented 2 years ago

If only we could just use argcomplete for ign! Unfortunately it does not currently use python.

There is a way to use argcomplete with programs not written in python using the --external-argcomplete-script, but it requires writing a python script with the argument parsing logic replicated using argparse.

scpeters commented 2 years ago

I've started in https://github.com/ignitionrobotics/ign-tools/pull/77 by porting a portion of the gz completion script that autocompletes from a list of valid ign commands determined with ign --commands. It is a minimal set of functionality to demonstrate the approach.

Bi0T1N commented 2 years ago

If only we could just use argcomplete for ign! Unfortunately it does not currently use python. ign uses Ruby to look for *.yaml files in the IGN_CONFIG_PATH and parses these YAML files to determine the list of commands that can be executed. These command are implemented as Ruby files that either dlopen a c++ library to call a function or pass the CLI arguments to a C++ executable.

Actually the Ruby class also supports shell completion. But is there any reason to stay with Ruby? The Ruby cmd classes could be implemented in Python with the same mechanism (argparse with argcomplete) as in ROS2. Also, I think there are more people familiar with Python than Ruby.
I'm not sure if this is the only use of Ruby in Ignition but replacing Ruby would also reduce the number of dependencies (Python is needed for the (new) scripting interface via pybind11 anyway).

chapulina commented 2 years ago

But is there any reason to stay with Ruby?

One reason at the moment is that we don't have the time to port it to another language, which would be a large effort, so we'd need a strong reason to do so.

I'm not sure if this is the only use of Ruby in Ignition

mabelzhang commented 2 years ago

Followup tasks to #87, which adds bash completion to second-level flags, i.e. cmd subcommand --flag:

azeey commented 1 year ago

As of https://github.com/gazebosim/gz-transport/pull/451, I believe all commands have bash completion now, so I'll go ahead and close this issue.