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

Replace environment variable lookup by --gui / --cli flag #22

Closed
Eolien55 opened this issue Oct 30, 2021 · 6 comments
Closed

Replace environment variable lookup by --gui / --cli flag #22

Eolien55 opened this issue Oct 30, 2021 · 6 comments

Comments

@Eolien55
Copy link

Thanks again for the work you've put into Klask.

I think that we should replace the environment variable KLASK_CHILD_APP by a --gui / --cli flag. This would allow to have only one binary for both graphical and command line interfaces.

Cheers

@MichalGniadek
Copy link
Owner

Hi, would something like this work for you?

use clap::{AppSettings, Arg, IntoApp, Parser};
use klask::Settings;

#[derive(Debug, Parser)]
struct App {
    #[clap(long)]
    gui: bool,
    foo: String,
    bar: String,
}

fn main() {
    // Use IgnoreErrors so we don't have to specify foo and bar when using "--gui"
    let a = App::into_app()
        .setting(AppSettings::IgnoreErrors)
        .get_matches();
    if a.is_present("gui") {
        // klask ignores any args names "help"
        let app = App::into_app().mut_arg("gui", |_| Arg::new("help"));
        // The closure in here will actually never run, because when this program is
        // run from gui, the "--gui" will always be false and the else path will be taken
        klask::run_app(app, Settings::default(), |_| {});
    } else {
        // The acutal code
        println!("{:?}", App::parse())
    }
}

@Eolien55
Copy link
Author

Well, this works, but I thought of it as the default feature for recognizing wether the program is a gui or started by the gui (opposed to having a environment variable to determine it). IMO, it's a better default, since turning a CLI program into a GUI one disables the CLI use, except in case where the user of klask do things like the one you proposed. I think it would be quite easy to modify the app to include the desired flags, and useful (I repeat myself, I know) since a GUI and a CLI live in the same binary.

I understand that Klask's raison-d'être is to turn CLIs into GUIs, but losing the CLI makes me a bit sad. I also understand that this isn't necessarily a feature used by all users. However, it would make Klask more appealing to me.

Anyway, thanks for you dedication. Have a great day !

@Eolien55
Copy link
Author

To be clear, I meant this as a crate-level feature, rather than a user-level one.

@MichalGniadek
Copy link
Owner

Sorry if I came off as dismissive in my previous answer.
I would expect most uses of klask are to make a cli app more accessible to people not experienced in command line. So based on this, making the gui just launch without any flags seems like the better default. And if someone needs to, there's the (kinda hacky) way to launch gui from cli in my answer above.
Also, previously I was using subcommands to differentiate between the main app and the child app, but it was changed to an environment variable because it was less invasive (e.g. when someone wanted to parse the arguments themselves).

Thanks for the interest in this library!

@Eolien55
Copy link
Author

Eolien55 commented Nov 4, 2021

Ok I get all of that. I agree, forcing CLI isn't a good idea, and adding options might be invasive. I would suggest the use of crate features for adding optional --cli flag rather than a --gui flag, or maybe using CLI flags to build GUI fields state ?

Maybe it's a bad idea ? I don't really know, and I hope I gave you some ideas.

I can't wait for clap v3 to come out so that I can use your crate ! Cheers

@MichalGniadek
Copy link
Owner

I just don't really see the point. But I remember that at the beginning I have looked for a way to detect if the app was launched from a command line or if it was launched from a gui. I couldn't really find anything, but I might try again because that would be the best option in my opinion (you could use cli in the first case and launch a gui in the second).
Thanks!

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

No branches or pull requests

2 participants