Discovering just
What is just
I recently stumbled upon this article about creating internal or personal cli’s, using just-cli. After give it a try, it seems to be a great tool if you want to create some kind of personal cli or keep a set of commands in one place. As a test run at work, I replaced a set of (small) batch files (most of them oneliners and twoliners) with a single justfile
.
My favorite practical features…
… compared to a simple set of multiple batch files:
- Commands can be documented, i prefer the syntax with an explicite
doc
attribute:
[doc("some description of a command")]
-
Building on the previous point,
just --list
prints a nicely formatted list of all commands defined in the justfile (along with their description). -
Using
[private]
as attribute on a just command, you can take the command out of the help/usage list. This is e.g. typically used on the default command, like this:
[private]
default:
just --list
- Different shells are supported and can be switched via a simple command, generally like this:
set shell := ["SHELLCOMMAND", "ARG1"]
or
set windows-shell := ["SHELLCOMMAND", "ARG1"]
- Arguments are by default defined by name (improving their documentational value), and are accessed via double braces, like this:
hello title name:
echo Hello, {{title}} {{name}}
- Support for variables, e.g. you can define a long path to a file once and use the short variable name over and over again.
Code snippets for reference
A basic empty justfile
# use cmd.exe instead of sh:
set windows-shell := ["cmd.exe", "/c"]
# use powershell.exe instead of sh:
# set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
[private]
@default:
just --list
[doc("print hello world-like")]
[positional-arguments]
[no-cd]
hello *args='':
echo Hello, {{args}}
Defining and using variables
Variables are set using the assignment operator :=
, variables, as well as command arguments, are accessed via double braces {{VAR}}
.
myprog := 'D:\SYS\Programs\my_cool_app\myapp.exe'
@mp *args='':
{{myprog}} {{args}}
For reference
What to achieve | Solution |
---|---|
Document a command | [doc("my command documentation")] |
Don’t show command in help | [private] as attribute on the command |
Don’t repeat commands as output | Put @ in front of the command name |
Run command exactly from where just is called | [no-cd] as attribute to the command |
Use positional arguments | Apply [positional-arguments] , and configure arguments as *args= , access the whole list as {{args}} |