Skip to content

Commit

Permalink
fix(sh): pass -h, --help to the script (#64)
Browse files Browse the repository at this point in the history
We want to prioritise printing the script's own help message over the
shell's help message. Therefore, modify `rbmk sh` to only print its own
help when the script is named `-h` or `--help` and otherwise pass
arguments to the script.
  • Loading branch information
bassosimone authored Dec 23, 2024
1 parent 85d9a47 commit c7cea98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions pkg/cli/sh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Run `SCRIPT` using a POSIX-compliant shell interpreter providing
to the script the given `ARGUMENTS`, which will be available to
the script as `$1`, `$2`, etc.

As a special case, if `SCRIPT` is `-h` or `--help`, the command
prints this help message and exits.

This shell implementation (based on `mvdan.cc/sh/v3`) is consistent
across operating systems and supports:

Expand Down Expand Up @@ -111,6 +114,9 @@ using `rbmk sh $script` precisely because of this issue.

## History

Since RBMK v0.12.0, the `-h, --help` flag is passed by default to the
`SCRIPT` rather than printing the `rbmk sh` command's help.

Since RBMK v0.10.0, it is possible to pass arguments to the script
executed by `rbmk sh` using the command line.

Expand Down
18 changes: 10 additions & 8 deletions pkg/cli/sh/sh.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ func (cmd command) Help(env cliutils.Environment, argv ...string) error {
}

func (cmd command) Main(ctx context.Context, env cliutils.Environment, argv ...string) error {
// 1. Honour requests for printing the help.
if cliutils.HelpRequested(argv...) {
return cmd.Help(env, argv...)
}

// 2. Ensure we have exactly one script to run.
// 1. Ensure we have exactly one script to run.
if len(argv) < 2 {
err := errors.New("expected a script with optional arguments")
fmt.Fprintf(env.Stderr(), "rbmk sh: %s\n", err.Error())
fmt.Fprintf(env.Stderr(), "Run `rbmk sh --help` for usage.\n")
return err
}

// 2. If the script is named `-h` or `--help` print help.
if argv[1] == "-h" || argv[1] == "--help" {
return cmd.Help(env, argv...)
}

// 3. Open and parse the shell script.
scriptPath := argv[1]
filep, err := env.FS().Open(scriptPath)
Expand All @@ -66,12 +66,14 @@ func (cmd command) Main(ctx context.Context, env cliutils.Environment, argv ...s
// scripts written before the release of RBMK v0.7.0.
os.Setenv("RBMK_EXE", "rbmk")

// 5. Create the shell interpreter.
// 5. Create the shell interpreter ensuring we properly use `--` to
// ensure options get passed to the script itself.
scriptParams := append([]string{"--"}, argv[2:]...)
runner, err := interp.New(
interp.StdIO(env.Stdin(), env.Stdout(), env.Stderr()),
interp.Env(expand.FuncEnviron(os.Getenv)),
interp.ExecHandlers(newBuiltInMiddleware()),
interp.Params(argv[2:]...),
interp.Params(scriptParams...),
)
if err != nil {
fmt.Fprintf(env.Stderr(), "rbmk sh: cannot create interpreter: %s\n", err.Error())
Expand Down

0 comments on commit c7cea98

Please sign in to comment.