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

Extract/add public method Error.exit_code #4967

Merged
merged 1 commit into from Jun 14, 2023

Conversation

hartwork
Copy link
Contributor

… and simplifies method Error.exit as a side effect.

The motivation for this pull request was to allow simplification of the code below (click to expand) where marked with XXX with future releases of clap.
// Copyright (c) 2023 Sebastian Pipping <sebastian@pipping.org>
// SPDX-License-Identifier: MIT

// ..snip..

// Matches the two internal constants from [clap_builder-4.3.1]/src/util/mod.rs  // XXX
const SUCCESS_CODE: i32 = 0;                                                     // XXX
const USAGE_CODE: i32 = 2;                                                       // XXX

fn main() {
    exit(middle_main(args_os()));
}

fn middle_main<I, T>(argv: I) -> i32
where
    // to match clap::Command.get_matches_from
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    let clap_result = command_line_parser::command().try_get_matches_from(argv);
    match clap_result {
        Ok(matches) => innermost_main(matches),
        Err(e) => {
            // This mimics clap::Error.exit minus the call to safe_exit
            let _ = e.print();
            if e.use_stderr() {                                                  // XXX
                USAGE_CODE                                                       // XXX
            } else {                                                             // XXX
                SUCCESS_CODE                                                     // XXX
            }                                                                    // XXX
        }
    }
}

fn innermost_main(matches: ArgMatches) -> i32 {
    // ..snip..
}

What do you think?

Simplifies method Error.exit as a side effect.
@hartwork hartwork changed the title Extract public method Error.exit_code Extract/add public method Error.exit_code Jun 13, 2023
Copy link
Member

@epage epage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, the main issue is that use_stderr is weird for callers to key off of for determining failure. This doesn't help with that when you want to change exit codes but does help if you are ok with the existing exit codes.

In terms of code bloat, this shouldn't add anything.

In terms of API bloat, looking through Error, this seems fine.

Probably the main question is whether to support ExitCode but that still seems like a relatively under-developed part of the stdlib to be trying to support here

@epage epage merged commit a3f482f into clap-rs:master Jun 14, 2023
21 checks passed
@hartwork
Copy link
Contributor Author

@epage thanks for the merge, cool! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants