Using bashly to create a CLI
source link: https://ylan.segal-family.com/blog/2023/02/17/using-bashly-to-create-a-cli/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Using bashly to create a CLI
Feb 17, 2023 • Ylan Segal • unix, bash
bashly is a command-line application that let’s you generate feature-rich command line tools. The idea is that you specify via a YAML file what subcommands, arguments, flags and environment variables you want for your executable, and bashly
takes care of generating all the boilerplate on a bash script, so that you can focus on your code. Many languages support similar via libraries, like optparse in ruby.
I recently used it to port a series of scripts for personal use that where all part of a series of commands I use to manage my personal note taking. I turned the all those separate scripts into a CLI with subcommands. Instead of zk_title
and zk_today
, I know have zk title
and zk today
, among others).
Here are my observations:
- The documentation is well done. In particular the examples showed me how to do everything I needed.
- The ability to check for required environment variables was very useful. If only a particular command requires a certain environment variable, that can be configured too.
- Reading from
stdin
or from a file is a very common use case. It’s well supported. - Commands can be aliased to shorter names.
- Flag handling is great. Short flags can be combined (i.e.
zk title -ps
instead ofzk title -p -s
) - Each command lives in it’s own file. If needed, custom functions that are called from other commands are supported.
- Some of my previous commands were written in Ruby.
bashly
supports heredocs, which make it possible to continue using ruby for portions of your script, albeit this is a bit of a hack and makes the script less portable:
/usr/bin/env ruby - ${arguments} <<-RUBY
puts "hello #{ARGV}"
RUBY
Note that for heredocs to work, the following environment variable needs to be set BASHLY_TAB_INDENT=1
.
Overall, I was happy with the results. All the boilerplate code like creating global and command --help
output, argument and environment variable checking, and flag handling was abstracted away.
Find me on Twitter at @ylansegal and Mastodon at @[email protected]
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK