Outreachy: Bash Completion

Prior to applying for Outreachy, one of the points that drew me to Fedora’s Bodhi project was the proposed idea of bash completion for the Bodhi command line interface (CLI). For those unfamiliar with bash completion, it is the phenomenon that occurs when you begin to type a command into your Unix shell (in this case: bash), press tab, and bash either (1) autocompletes the command for you or (2) provides a list of all of the possible ways to complete that command. During my undergrad CS studies, my favorite projects entailed creating Unix shells, command line chat servers, and text-based command line games. All of these projects have a clear common denominator: the command line. I found that I would spend hours upon hours blissfully trying to perfect my CLI’s user interface. Thus, this seemed to be the Outreachy project of my dreams!

To implement Bodhi’s CLI, Bodhi uses Click, which is a cool Python package used to create CLIs. If you aren’t very familiar with Bodhi, the majority of the code is written in Python. Prior to my internship, I was not well-versed in Python let alone Python libraries or Python packages (I had only ever used it to write small scripts, test algorithms, and create servers).

Because of the nifty framework Click, the bash completion for Bodhi proved simpler (far simpler) than I anticipated. Click actually already supports a certain level of bash completion for its CLIs. All that was required was to generate the activation script and to get around the Python errors. Bodhi now ships with the bash-complete activation script, so all you need to do is press tab! The PR enabling bash completion can be found here.

 

What does Bash completion look like within the Bodhi CLI?

Screenshot from 2017-07-28 04-42-35

Bash completion supports the following commands:

  • bodhi
    • updates
      • subcommands: comment, download, edit, new, query, request
        • each subcommand’s respective parameters (too many to list)
    • overrides
      • subcommands: edit, query, save
        • each subcommand’s respective parameters (too many to list)

 

Enabling Bash Completion

It is enabled by default. Just type the following* into your Vagrant instance’s shell:

bodhi <TAB><TAB>

*Replace “<TAB><TAB>” with two quick taps of the tab key ;). Oh, and yes — there’s a much-needed space in between bodhi and <TAB>!

 

Uh oh! Why am I seeing loads of warnings?

For bash completion to work properly (i.e., without various Python errors printing to your screen each time you press the tab key), you will need to ignore Python warnings. Otherwise, your output will look like this:

Screenshot from 2017-07-28 04-22-51

 

Ignoring Python warnings

Python warnings tend to be helpful in a dev environment, so proceed at your own risk of future frustration. You can either ignore all Python warnings, or you can temporarily ignore Python warnings while using bash completion.

To ignore all Python warnings, the user could change the following line in their .bashrc:

export PYTHONWARNINGS="once"

to

export PYTHONWARNINGS="ignore"

Again, as a dev, you probably don’t want to ignore Python warnings everywhere. It may be a good idea to only ignore the warnings within the Bodhi CLI. If you’d still like to see the Python warnings in outside of the Bodhi CLI, you can use the following (somewhat hacky) solution by editing the bodhi-complete.sh file:

Screenshot from 2017-07-28 05-17-19

 

Scalability and future improvements

Earlier I mentioned that Click only supports a certain level of bash completion. Click has yet to implement the ability of dynamic completion. Thus, because Bodhi relies on Click for its CLI and thus bash completion, Bodhi also has yet to implement the ability of dynamic completion. This feature would look like this:

bodhi updates new bodhi-2.7.0-<TAB>

where the auto complete options would result in the following

bodhi updates new bodhi-2.7.0-
fc25 fc26

Though, for Bodhi to use dynamic completion, it would require changes to upstream Click. Unfortunately, I cannot find evidence that Click has plans or desire to add this feature anytime soon.

 

Reflection and lessons learned

After learning how simple it is to create CLIs with Click, I am inspired to port some of my favorite projects (originally written in C) to Python. Although I am still discovering many of its nuances, Python is quickly becoming my favorite programming language. I still have some leftover habits of writing Python as though it’s C and thus may occasionally find myself not realizing a simpler way of approaching a certain line of code, but that’s part of what makes learning Python great: consistently finding yourself shocked at its simplicity. And lucky for me — I have a very helpful mentor!

Keep in mind:

  1. There may be a simpler way of adding a new feature than you initially imagined, so before coding, be sure to search for external packages, tools, and libraries — and most importantly: check with your mentor!
  2. An Outreachy project may not be exactly what you anticipate — and that’s okay! — so it may be a good idea to vest interest in more than one project idea.
  3. You don’t have to be extremely well-versed in an open source project’s main language (though it will certainly save you quite a bit of time)
  4. If you want a feature in open source project A which depends on open source project B, it may require contributing to open source project B, which could cause hiccups.

 

Many thanks to my very helpful, patient, and encouraging mentor, bowlofeggs!