Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub 2.0 #16

Merged
merged 87 commits into from
May 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
9e9c23a
chore: Fix file mode
juanibiapina May 9, 2024
41263b8
test: Improve arguments test
juanibiapina May 9, 2024
5445bda
refactor: Replace tuple with struct
juanibiapina May 9, 2024
ee9e52e
refactor: Split FileCommand and DirectoryCommand from ExternalCommand
juanibiapina May 9, 2024
3720603
refactor: Hide FileCommand fields
juanibiapina May 9, 2024
8822ec5
feat: Introduce usage parser
juanibiapina May 9, 2024
ec1566e
feat: Add basic error handling for invalid usage strings
juanibiapina May 10, 2024
96ba153
refactor: Split new Usage parser to its own module
juanibiapina May 10, 2024
afa0b8b
refactor: Improve usage parser
juanibiapina May 10, 2024
f92ff28
test: Remove redundant test
juanibiapina May 10, 2024
769e491
test: Improve tests for commands
juanibiapina May 10, 2024
b2b9891
test: Change top level help test to not break when adding new fixtures
juanibiapina May 10, 2024
08503f7
feat: Add error handling for invalid usage strings
juanibiapina May 11, 2024
33ca46d
test: Separate fixtures for help command
juanibiapina May 11, 2024
9f29302
test: Separate fixture for completions tests
juanibiapina May 11, 2024
e9408c3
test: Remove usage line from echo fixtures
juanibiapina May 11, 2024
d8dd4bb
test: Add passing integration test for future usage feature
juanibiapina May 13, 2024
f8ce5f1
test: Remove usage line from fixture command
juanibiapina May 13, 2024
265d04e
refactor: Extract main error handling code
juanibiapina May 13, 2024
442306f
feat!: Require -- before cli arguments
juanibiapina May 13, 2024
7d31438
feat: Rename commands argument to cliargs since it can include flags
juanibiapina May 13, 2024
c2a1881
feat: Capture cliargs as raw
juanibiapina May 13, 2024
48c66c7
chore: Configure tmux dev session
juanibiapina May 13, 2024
e81c59e
refactor: Improve naming
juanibiapina May 13, 2024
84f96de
refactor: Move help implementation to trait
juanibiapina May 13, 2024
1093c87
feat: Introduce top level --usage flag for printing usage
juanibiapina May 13, 2024
c20f273
feat!: Replace help command with --help flag
juanibiapina May 13, 2024
1003d41
feat: Add support for short help flag (-h)
juanibiapina May 14, 2024
55426ed
refactor: Move some initialization code to Config
juanibiapina May 14, 2024
bee78aa
refactor: Extract parsing of user cli arguments
juanibiapina May 14, 2024
1f4fa15
refactor: Inline a couple of functions
juanibiapina May 14, 2024
0925991
feat!: Replace commands internal subcommand with --commands flag
juanibiapina May 14, 2024
c0835cb
feat!: Replace completions subcommand with --completions flag
juanibiapina May 15, 2024
54df9ea
refactor: Remove old usage parsing
juanibiapina May 15, 2024
7478094
refactor: Refactor error handler function
juanibiapina May 15, 2024
24773a7
refactor: Reorganize file command usage generation
juanibiapina May 15, 2024
fbd7248
refactor: Hide fields in command types
juanibiapina May 15, 2024
b4e87a8
refactor: Simplify silent logic
juanibiapina May 15, 2024
708d19a
feat!: Rework Usage
juanibiapina May 15, 2024
c4e7494
feat!: Rework default usage
juanibiapina May 15, 2024
b9a3bb7
feat!: Validate arguments to file subcommands
juanibiapina May 15, 2024
b3580c5
refactor: Remove unused arguments
juanibiapina May 15, 2024
902b4d5
feat: Do not rely on clap usage generation
juanibiapina May 15, 2024
10a3078
feat!: Generate usage using clap for file commands
juanibiapina May 15, 2024
7776116
feat!: Generate top level command usage with clap
juanibiapina May 15, 2024
3336f09
refactor: Rename method
juanibiapina May 15, 2024
b9b0292
refactor: Move user_cli_command to Config
juanibiapina May 15, 2024
63a82de
refactor: Merge top level and directory commands into one
juanibiapina May 15, 2024
79f3fe1
refactor: Improve parser
juanibiapina May 15, 2024
590688a
refactor: Improve usage for directory commands
juanibiapina May 15, 2024
9675c7c
refactor: Make doc parts optional
juanibiapina May 15, 2024
83a133b
refactor: Remove duplication
juanibiapina May 15, 2024
31f2df9
feat: Rewrite help for file commands using clap
juanibiapina May 15, 2024
8b90e2a
fix: Fix bug where directory commands were taking arguments for the t…
juanibiapina May 16, 2024
5317aef
feat: Generate directory subcommand help using clap
juanibiapina May 16, 2024
2c5868b
refactor: Remove dead code
juanibiapina May 16, 2024
d427f68
feat!: Add basic argument parser
juanibiapina May 17, 2024
f32bb4b
fix: Allow Usage strings without rest arguments
juanibiapina May 17, 2024
47765db
feat: Add option to infer long arguments
juanibiapina May 17, 2024
3772de2
feat: Expose parsed arguments to scripts using env variable
juanibiapina May 17, 2024
50d98f7
feat: Add support for values in usage flags
juanibiapina May 21, 2024
f98c31a
refactor: Remove duplication
juanibiapina May 21, 2024
30891a7
feat: Add support for exclusive arguments
juanibiapina May 21, 2024
9e6715f
test: Improve test for nested commands
juanibiapina May 22, 2024
b9d5305
refactor: Delay usage validation until it is needed
juanibiapina May 22, 2024
2e84300
refactor: Remove duplication in summary and help generation
juanibiapina May 22, 2024
4078946
refactor: Directory command constructor cannot fail
juanibiapina May 22, 2024
499c4e6
feat: Add usage validation
juanibiapina May 22, 2024
e0855d1
feat: Change syntax for positional arguments to match clap output
juanibiapina May 22, 2024
f7286c1
feat: Allow more characters in usage options
juanibiapina May 22, 2024
fc95297
feat: Improve arguments and usage
juanibiapina May 22, 2024
5bbd8a9
fix: Handle case where libexec is not a directory
juanibiapina May 22, 2024
ad53e0f
fix: Relative is required when bin is used
juanibiapina May 22, 2024
ca43518
feat: Rename bin argument to executable
juanibiapina May 22, 2024
3757cf4
doc: Add description and key features section to README
juanibiapina May 22, 2024
c3f252d
doc: Add installation section to README
juanibiapina May 22, 2024
b46240b
doc: Add note on table of contents
juanibiapina May 22, 2024
0a0c6ba
doc: Update README
juanibiapina May 22, 2024
cfc14d1
doc: Add more info on migrating to sub 2.x
juanibiapina May 22, 2024
85e8bb6
doc: Improve readme
juanibiapina May 23, 2024
8c65ea9
doc: Update readme
juanibiapina May 23, 2024
e6d0a7a
doc: Add aliases section to readme
juanibiapina May 23, 2024
285c39f
doc: Improve syntax highlighting on readme
juanibiapina May 23, 2024
7866742
doc: Expand aliases section
juanibiapina May 23, 2024
77e6896
doc: Document nested subcommands
juanibiapina May 23, 2024
a35d2ab
doc: Add argument parsing and usage syntax to readme
juanibiapina May 23, 2024
93c0425
doc: Update changelog
juanibiapina May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
doc: Improve readme
  • Loading branch information
juanibiapina committed May 23, 2024
commit 85e8bb6d8b1c3f9e7c1ea93b6390518f2c4c88fe
124 changes: 80 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -55,96 +55,131 @@ Then add it to your packages:
}
```

## Usage
## Setup

### As an alias

The quickest way to get started with `sub` is to define an alias for your CLI
(let's call it `hat`) in your shell:

```sh
alias hat='sub --name hat --absolute /path/to/cli/root --'
```

`sub` is meant to be used as the entry point for a CLI. Given the following
directory structure:
Where `/path/to/cli/root` contains a `libexec` directory with executable
scripts, for example:

```
.
└── libexec
├── user-script1
├── user-script2
   └── user-script3
```

### As an executable

A more reliable way is to use an executable script as the entry point. Given
the following directory structure:

```
.
├── bin
│   └── awesomecli
│   └── hat
└── libexec
├── list
├── new
├── open
   └── nested
    ├── README
    └── command
├── user-script1
├── user-script2
   └── user-script3
```

The entry point in `bin/awesomecli` can then be:
The entry point in `bin/hat` is then:

```bash
#!/usr/bin/env bash

sub --name awesomecli --executable "${BASH_SOURCE[0]}" --relative ".." -- "$@"
sub --name hat --executable "${BASH_SOURCE[0]}" --relative ".." -- "$@"
```

The `--name` argument tells `sub` the name of the CLI. This is used when
printing help information.

The `--executable` argument tells `sub` where the CLI entry point is located.
Usually this will just be `${BASH_SOURCE[0]}`. The `--relative` argument tells
`sub` how to find the root of the CLI starting from the CLI entry point.
This will almost always be `${BASH_SOURCE[0]}`. The `--relative` argument tells
`sub` how to find the root of the CLI starting from the CLI entry point. In the
line above, just replace `hat` with the name of your CLI.

After the root directory is determined, `sub` picks up any executable files in
a `libexec` directory inside root to use as subcommands. Directories create
nested subcommands. Arguments for the subcommands themselves go after the `--`.
## Usage

With this setup, invoking `awesomecli` will display all available subcommands
with associated help information. To invoke a subcommand, use:
Once you have set up your CLI, you can get help by running:

```sh
$ hat --help
```
```
$ awesomecli <subcommandname> <args>
Usage: hat [OPTIONS] [commands_with_args]...

Arguments:
[commands_with_args]...

Options:
--usage Print usage
-h, --help Print help
--completions Print completions
--commands Print subcommands
--extension <extension> Filter subcommands by extension

Available subcommands:
user-script1
user-script2
user-script3
```

Or to invoke a nested subcommand:
To invoke a subcommand, use:

```
$ awesomecli nested command
$ hat user-script1
```

## Documenting commands

To get help for a command, use the built in `--help` flag:

```sh
awesomecli --help <commandname>
hat --help <commandname>
```
or
```sh
awesomecli <commandname> --help
hat <commandname> --help
```

## Documenting commands

In order to display help information, `sub` looks for special comments in the
corresponding script. An example documented command:
corresponding script. A fully documented `hello` script could look like this:

```sh
#!/usr/bin/env bash
#
# Summary: One line summary of the command
# Summary: Say hello
#
# Usage: {cmd} <positional-arg>
# Usage: {cmd} <name> [--spanish]
#
# Extended description of what the command does.
# Say hello to a user by name.
#
# The extended description can span multiple lines.
# With the --spanish flag, the greeting will be in Spanish.

set -e

echo "Hello $1"
```
declare -A args="($_HAT_ARGS)"

If the command is a directory, `sub` looks for documentation in a `README` file
inside that directory.
if [[ "${args[spanish]}" == "true" ]]; then
echo "¡Hola, ${args[name]}!"
else
echo "Hello, ${args[name]}!"
fi
```

## Sharing code between scripts

When invoking subcommands, `sub` sets an environment variable called
`_CLINAME_ROOT` (where `CLINAME` is the name of your CLI. This variable holds
the canonicalized path to the root of your CLI. It can be used for instance for
sourcing shared scripts from a `lib` directory next to `libexec`:
`_HAT_ROOT` (where `HAT` is the capitalized name of your CLI. This variable
holds the canonicalized path to the root of your CLI. It can be used for
instance for sourcing shared scripts from a `lib` directory next to `libexec`:

```sh
source "$_CLINAME_ROOT/lib/shared.sh"
@@ -153,12 +188,13 @@ source "$_CLINAME_ROOT/lib/shared.sh"
## Caching

When invoking subcommands, `sub` sets an environment variable called
`_CLINAME_CACHE` (where `CLINAME` is the name of your CLI. This variable points
to an XDG compliant cache directory that can be used for storing temporary files.
`_HAT_CACHE` (where `HAT` is the capitalized name of your CLI. This variable
points to an XDG compliant cache directory that can be used for storing
temporary files.

## Migrating to Sub 2.x

### --bin was renamed to --executable
### change --bin was to --executable

The `--bin` argument was renamed to `--executable` to better reflect its purpose.

Loading