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

Bug: Command-line interface generator exits with success code 0 even when you pass random jibberish in the arguments #5514

Closed
unclechu opened this issue Jan 17, 2024 · 2 comments
Labels
CLI perl6 executable and user programs command-line interface

Comments

@unclechu
Copy link

The Problem

Take for instance test.raku:

#! /usr/bin/env raku
use v6.d;

multi MAIN('foo') { say 'foo subcommand handler' }
multi MAIN('bar') { say 'bar subcommand handler' }

sub USAGE {
  print $*USAGE ~ "\n\n" ~ qq:to/USAGE/;
  Some example text
  USAGE
}

When I call ./test.raku --help I get the usage info and the exit code is success (0) which is expected, as I correctly and explicitly requested usage info by --help. But when I call ./test.raku random-text or just ./test.raku I get the same usage info but it exits with success (0) while it should be error non-zero exit code it’s arguments parsing failure.

Expected Behavior

When calling ./foo.raku or ./foo.raku random-jibberish to get a non-zero (error) exit code.

Actual Behavior

I get successful exit code (0).

Steps to Reproduce

  1. Save the example from “The Problem” section above to test.raku file and chmod +x test.raku

  2. Run this:

    $ ./foo.raku
    Usage:
      ./foo.raku foo
      ./foo.raku bar
    
    Some example text
    $ echo $?
    0
    $ ./foo.raku any-random-text
    Usage:
      ./foo.raku foo
      ./foo.raku bar
    
    Some example text
    $ echo $?
    0

Environment

  • Operating system: NixOS 23.11.2936.428544ae95ee (Tapir)
  • Compiler version (rakudo -v or raku -v):
    $ rakudo -v
    Welcome to Rakudo™ v2023.08.
    Implementing the Raku® Programming Language v6.d.
    Built on MoarVM version 2023.09.
@ab5tract ab5tract added the CLI perl6 executable and user programs command-line interface label Jan 22, 2024
ab5tract added a commit that referenced this issue Apr 27, 2024
Even though it's an older approach, it is trivial
to provide `USAGE` the same exit code logic as for
`GENERATE-USAGE`:

  If a `--help` is part of the invocation, exit 0.

  Otherwise, exit 2.

Addresses R#5514 (#5514).
@ab5tract
Copy link
Collaborator

USAGE is the older way to define things. In the code there is a comment mentioning that a deprecated message should be shown but it looks like this has not yet been added.

The newer approach is GENERATE-USAGE(&main, |capture) {...}, which is more flexible but also more complicated. However, it will print to STDERR and exit with status 2 if triggered without --help.

Since USAGE leaves the user in charge of printing -- and doesn't provide access to passed arguments -- there is no way to conditionally divert the erroneous cases to STDERR.

However, at first glance it does seem trivial to at least conditionally set the exit status for USAGE, so I've submitted a PR to do just that.

ab5tract added a commit that referenced this issue Apr 28, 2024
Even though it's an older approach, it is trivial
to provide `USAGE` the same exit code logic as for
`GENERATE-USAGE`:

  If a `--help` is part of the invocation, exit 0.

  Otherwise, exit 2.

Addresses R#5514 (#5514).
@ab5tract
Copy link
Collaborator

@unclechu This should be fixed as of a4c7d5d. Happy hacking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI perl6 executable and user programs command-line interface
Projects
None yet
Development

No branches or pull requests

2 participants