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
feat!: Replace help command with --help flag
juanibiapina committed May 13, 2024
commit c20f273dec4ae13bccb04dff5202d371abab7939
1 change: 0 additions & 1 deletion integration/commands.bats
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ load test_helper
b
c.other
commands
help
nested"
}

21 changes: 10 additions & 11 deletions integration/help.bats
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@

load test_helper

@test "help: without arguments, displays help for top level command" {
@test "help: displays help for top level command" {
fixture "commands"

run main help
run main --help

assert_success
assert_output "Usage: main [<subcommands>] [<args>]
@@ -21,14 +21,13 @@ Available subcommands:
b
c.other
commands List available commands
help Display help for a sub command
nested "
}

@test "help: displays usage for a non documented command" {
fixture "project"

run main help no-doc
run main --help no-doc

assert_success
assert_output "Usage: main no-doc"
@@ -37,7 +36,7 @@ Available subcommands:
@test "help: displays help for a subcommand" {
fixture "project"

run main help with-help
run main --help with-help

assert_success
assert_output "Usage: main with-help
@@ -52,7 +51,7 @@ The help section can span multiple lines."
@test "help: displays summary for subcommand if help is not available" {
fixture "project"

run main help only-summary
run main --help only-summary

assert_success
assert_output "Usage: main only-summary
@@ -63,7 +62,7 @@ Return with error 4"
@test "help: fails gracefully when requested command doesn't exist" {
fixture "project"

run main help not-found
run main --help not-found

assert_failure
assert_output "main: no such sub command 'not-found'"
@@ -72,7 +71,7 @@ Return with error 4"
@test "help: displays help for a directory command" {
fixture "project"

run main help directory
run main --help directory

assert_success
assert_output "Usage: main directory [<subcommands>] [<args>]
@@ -91,7 +90,7 @@ Available subcommands:
@test "help: displays help for a nested subcommand" {
fixture "project"

run main help directory with-help
run main --help directory with-help

assert_success
assert_output "Usage: main directory with-help
@@ -106,7 +105,7 @@ The help section can span multiple lines."
@test "help: displays help for a double nested directory command" {
fixture "project"

run main help directory double
run main --help directory double

assert_success
assert_output "Usage: main directory double [<subcommands>] [<args>]
@@ -124,7 +123,7 @@ Available subcommands:
@test "help: displays help for a double nested sub command" {
fixture "project"

run main help directory double with-help
run main --help directory double with-help

assert_success
assert_output "Usage: main directory double with-help
4 changes: 2 additions & 2 deletions integration/nested.bats
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ load test_helper
run main nested

assert_success
assert_output "$(main help nested)"
assert_output "$(main --help nested)"
}

@test "nested: with a non existent subcommand, displays error message" {
@@ -35,5 +35,5 @@ load test_helper
run main nested double

assert_success
assert_output "$(main help nested double)"
assert_output "$(main --help nested double)"
}
2 changes: 1 addition & 1 deletion integration/subcommands.bats
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ load test_helper
run main

assert_success
assert_output "$(main help)"
assert_output "$(main --help)"
}

@test "subcommands: ignores hidden files" {
11 changes: 11 additions & 0 deletions integration/usage.bats
Original file line number Diff line number Diff line change
@@ -2,6 +2,17 @@

load test_helper

@test "usage: conflicts with help" {
fixture "project"

run main --usage --help

assert_failure
assert_output "error: the argument '--usage' cannot be used with '--help'

Usage: main --usage [commands_with_args]..."
}

@test "usage: when command has no Usage docstring prints default usage" {
fixture "project"

6 changes: 3 additions & 3 deletions src/commands/directory.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ use crate::parser;
use crate::error::{Error, Result};
use crate::commands::Command;
use crate::commands::internal::commands::internal_commands;
use crate::commands::internal::help::internal_help;
use crate::commands::external_subcommand;

pub struct DirectoryCommand<'a> {
@@ -86,7 +85,8 @@ impl<'a> Command for DirectoryCommand<'a> {
return Err(Error::UnknownSubCommand(self.names.last().unwrap().to_owned()));
}

let help_command = internal_help(self.config, self.names.clone());
help_command.invoke()
println!("{}", self.help());

Ok(0)
}
}
25 changes: 0 additions & 25 deletions src/commands/internal/help.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/commands/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ use crate::error::Result;
use crate::config::Config;
use crate::commands::Command;

pub mod help;
pub mod commands;
pub mod completions;

2 changes: 0 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ use crate::config::Config;
use crate::commands::file::FileCommand;
use crate::commands::directory::DirectoryCommand;
use crate::commands::toplevel::TopLevelCommand;
use crate::commands::internal::help::internal_help;
use crate::commands::internal::commands::internal_commands;
use crate::commands::internal::completions::internal_completions;
use crate::error::Result;
@@ -78,7 +77,6 @@ pub fn subcommand(config: &Config, mut cliargs: Vec<String>) -> Result<Box<dyn C
let name = &cliargs[0];

match name.as_ref() {
"help" => Ok(Box::new(internal_help(config, cliargs.split_off(1)))),
"commands" => Ok(Box::new(internal_commands(config, cliargs.split_off(1)))),
"completions" => Ok(Box::new(internal_completions(config, cliargs.split_off(1)))),
_ => {
7 changes: 3 additions & 4 deletions src/commands/toplevel.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ use crate::config::Config;
use crate::error::Result;
use crate::parser;
use crate::commands::Command;
use crate::commands::internal::help::internal_help;
use crate::commands::internal::commands::internal_commands;
use crate::commands::external_subcommand;

@@ -61,7 +60,6 @@ impl<'a> Command for TopLevelCommand<'a> {
}
}

subcommands.push(Box::new(internal_help(self.config, Vec::new())));
subcommands.push(Box::new(internal_commands(self.config, Vec::new())));

subcommands.sort_by(|c1, c2| c1.name().cmp(c2.name()));
@@ -75,7 +73,8 @@ impl<'a> Command for TopLevelCommand<'a> {
}

fn invoke(&self) -> Result<i32> {
let help_command = internal_help(self.config, Vec::new());
help_command.invoke()
println!("{}", self.help());

Ok(0)
}
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -39,6 +39,8 @@ fn main() {

let user_cli_command = Command::new(&config.name).no_binary_name(true).disable_help_flag(true)
.arg(Arg::new("usage").long("usage").num_args(0).help("Print usage"))
.arg(Arg::new("help").long("help").num_args(0).help("Print help"))
.group(ArgGroup::new("help_group").args(["usage", "help"]).multiple(false).required(false))
.arg(Arg::new("commands_with_args").trailing_var_arg(true).allow_hyphen_values(true).num_args(..));

let args = match user_cli_command.try_get_matches_from(sub_cli_args.cliargs) {
@@ -61,6 +63,11 @@ fn main() {
exit(0);
}

if args.get_one::<bool>("help").cloned().unwrap_or(false) {
println!("{}", subcommand.help());
exit(0);
}

match subcommand.invoke() {
Ok(code) => exit(code),
Err(error) => handle_error(&config, error),