-
Notifications
You must be signed in to change notification settings - Fork 152
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
Weird bool behaviour when not defined as a flag parameter #212
Comments
Can you give an example of what you were doing, to be sure I understand correctly? |
Sure: In |
So, an error saying "bool without short or long is meaningless"? |
That would work. Or we could have proper bool parsing. 😅 |
Which results do you expect from the linked code? Passing |
@TeXitoi Yes, exactly that. |
Idea:
This approach would allow us to support a lot of types almost out of box, like AtomicBool, BTreeSet, whatever with no black magic at all |
See also #79 |
@therealprof type Flag = bool;
#[derive(Structopt)]
struct Opt {
#[structopt(parse(from_str))]
flag: Flag
} |
Cool! |
... but it does not work:
|
Can you give an example? This is working: use structopt::StructOpt;
type Bool = bool;
#[derive(StructOpt, Debug)]
struct Opt {
verbose: Bool,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
} |
Ha, That's |
So use the typedef but get rid of the custom parser? |
That's proc macro, so no type information, just checking that the type of the field is stringly |
That works. |
Could someone please explain what this issue is for? |
Is this mentioned in the documentation somewhere? |
@therealprof Fair enough, it should be. |
I'm working on a PR to add it to the README. |
@therealprof Err, I'm working on it too 😄 . OK, go make you PR and I'll adjust mine to it. Btw, documentation goes to |
I was about to add it to the "basic" example. Seems to be useful and confusing enough to warrant appearance on the front page. |
@therealprof This is what you think but it doesn't look like. You see, this issue has been here for ~5 months and it got no 👍 likes at all, so I would say it's not really common use-case. We all think our use case is special but this is oftentimes wrong. I think a separate file in |
Regular bool types are only flags and their parsed value represents whether they were present on the command line or absent. However it is very useful to have true boolean argument parsing which can be combined with `Option` or default values. Closes TeXitoi#212
I disagree. Just because no one chimed in on this use case here doesn't mean it's not relevant. I haven't even noticed that what I intended to do didn't even work until a lot later... I've used boolean parameters at least as often if not more often in non-Rust projects than flags. On the contrary I not quite happy about the attitude that because you see this as totally and completely obvious it might not be a stumbling block for others... Anyway I'll leave it up to the maintainers to figure out relevance of this and whether they'd like to apply it to the basic example or not. |
I never said that it's not relevant, I said it's not common. I was trying to show you the reason I consider it a corner case by this like-counting. If someone out there thinks I'm wrong I'd be happy to listen.
Please don't ascribe me words I've never said, I genially apologize if the wording was not clear enough. // A flag, true if used in the command line. Note doc comment will
// be used for the help message of the flag. The name of the
// argument will be, by default, based on the name of the field.
/// Activate debug mode
#[structopt(short, long)]
debug: bool, What you're asking for is a way to avoid this special casing, i.e just the contrary.
Based on my subjective experience your case is a corner one. Based on your subjective experience you're case is important enough to be mentioned and I agree, but I don't agree that it should be mentioned on the front page, only common cases go there. Of course we can't apply judgement based only on subjective opinions, we [maintainers] need to be objective.
I am one of the maintainer here, you can ask/argue with me If you have any other arguments, but please try to be objective. @TeXitoi what do you think about it? |
I for one am. I don't think "like counting" is representative for anything unless you're expressly polling for likes which is kind of nonsense...
It is absolutely not clear what that means. In hindsight I do understand what is happening but just from reading this example it is definitely not and the documentation doesn't explicitly mention it either. If no one came up with the "type Bool = bool;" suggestion I'd have never figured this out at all.
I do think there should be an example of every basic type in this basic example, true boolean parameters are such a basic type just like the others are. |
This is the best we've got. It's not like "pooling for likes", it's about "is there many people interested in the topic". We can't put every single neat trick in the readme or main doc page or it would grow so large so it'd become unusable/unclear. We have to choose and filter out only simple and common use cases, there are examples for anything else. Also, the very fact that it's only you (one man) and maintainers are participating in this discussion and no likes involved represents unpopularity of the topic quite clearly. And by the way, about the "nonsense" thing: there is rust-embedded/wg#351 , it was done via like pooling and you were among people behind this RFC 😄
There's a table of special types in the doc along with description of every one of them. If you think this isn't clear enough I'm open to proposals, but I really don't think populating corner cases' explanations in the basic example would help the matter.
There is such an example of You want to use "true"/"false" parsing does not come out of box with
Well, you misunderstood the doc/example which explicitly shows you that |
Actually, we could support "true"/"false" out of box parsing when |
First, please calm down. We are non native English speakers from different cultures, and we might interpret wrongly the tone of the others. It was the Care Bears interlude. The README is an advertisement, or a quickstart guide. Not everything is present inside it. For example, subcommands, that are used a lot, are not present in it. So the README is not really appropriate to talk about that. About common usage of parameters/options taking Now, that's important to have solution to allow this use case, and document it as the bool case is quite central in structopt. It can be particularly tricky to do that in a non error-prone way. I propose this as a solution: The The option taking bool will be documented like that (already working): use structopt::StructOpt;
#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(long, parse(try_from_str))]
verbose: bool,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
} That's, IMHO, much better that the type alias workaround. This parameter version must fail to compile with, as message, something like "Using bool as a parameter might not be what you want. Maybe you want to create a flag? In this case, add Fail to compile (but compile today): use structopt::StructOpt;
#[derive(StructOpt, Debug)]
struct Opt {
verbose: bool,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
} Compile with the expected behavior (and is already working today): use structopt::StructOpt;
#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(parse(try_from_str))]
verbose: bool,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
} For documentation, I propose an example, and a note in the documentation around What do you think? |
I think Care Bears are adorable. As regards to the proposal - I'm 100% agree. @therealprof Does this proposal suit your needs (and do you agree Care Bears are cherish?)? |
@TeXitoi Sounds good. However I thought that's how I implemented originally and it didn't work as intended. Will have to retest when I find the time. Re examples: Traditionally GNU projects tend to use Two popular examples I happen to know from the top of my head which support it are atom and VSCode. More sophisticated It's just a matter of preference. |
Your buggy version was the one that is specked as "doesn't compile". |
I tried a lot of things. I'm note quite certain anymore whether |
Okay, works a treat. Going to change my implemtations, thanks! |
I've been (incorrectly) using
bool
arguments without any option under the assumption that the mandatory value is actually parsed and turned into a properbool
according to what was passed on the CLI but I just found out that it is alwaystrue
. This makesbool
parameters which are not flags rather useless; IMHO they either need to be really parsed or there should be a compiler error saying:You've been trying to use a bool parameter which is not a flag and hence will be always true after evaluation which is probably not what you want
.The text was updated successfully, but these errors were encountered: