-
Notifications
You must be signed in to change notification settings - Fork 10
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
Use different crate for argument parsing #92
Comments
Not to forget that bash completion should come much easier when we use this crate. |
Please have a look at my
But the code is a lot shorter and a lot more readable – and it should get even better if we improve the Here are some measurements of the code and binary size:
I think that’s acceptable. |
Thanks for working on this issue! Indeed, it looks nicer. Yes, I agree the size increase is acceptable. Not a full review (I gather you are not done yet), but few preliminary comments:
Indeed, that would be awesome! Let me know if you need help with that. |
Yes, I wrote that comment when I deleted the previous std* code and was not yet aware of structopt’s behavior. It turns out that, contrary to
I tried to consistently use third person because that’s what structopt does for the auto-generated commands and options. Personally, I have a slight preference for imperatives.
No complications, similar to the other TODO comment.
I haven’t used
If you have an easy solution to get this code to work, that would be nice: Command! {OtpCommand, [
/// Prints the Nitrokey configuration
Get => |ctx| commands::config_get(ctx),
/// Changes the Nitrokey configuration
Set(ConfigSetArgs) => config_set,
]} |
I fixed the two issues mentioned earlier (enum variants in help text; exit handling), squashed some commits, added more explanations to the commit messages and opened a pull request (#97) for in-depth discussion of the implementation. |
Yes, different issue. I just brought it up because it may help in the transition (no [Indeed, checking the latest revision you did add it]
What I was trying to say is that in an impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::CommandError(ref err) => Some(err),
Error::CommunicationError(ref err) => Some(err),
Error::ConcurrentAccessError => None,
Error::LibraryError(ref err) => Some(err),
Error::PoisonError(ref err) => Some(err),
Error::RandError(ref err) => Some(err.as_ref()),
Error::UnexpectedError => None,
Error::UnknownError(_) => None,
Error::Utf8Error(ref err) => Some(err),
}
}
}
That...was...intense o_O But should work with the following patch: --- nitrocli/src/arg_util.rs
+++ nitrocli/src/arg_util.rs
@@ -24,12 +24,18 @@ macro_rules! count {
}
}
+/// Translate an optional source into an optional destination.
+macro_rules! tr {
+ ($dst:tt, $src:tt) => { $dst };
+ ($dst:tt) => { };
+}
+
macro_rules! Command {
- ( $name:ident, [ $( $var:ident($inner:ident) => $exec:expr, ) *] ) => {
+ ( $name:ident, [ $( $var:ident$(($inner:ty))? => $exec:expr, ) *] ) => {
#[derive(Debug, PartialEq, structopt::StructOpt)]
pub enum $name {
$(
- $var($inner),
+ $var$(($inner))?,
)*
}
@@ -41,7 +47,7 @@ macro_rules! Command {
) -> crate::Result<()> {
match self {
$(
- $name::$var(args) => $exec(ctx, args),
+ $name::$var$((tr!(args, $inner)))? => $exec(ctx $(,tr!(args, $inner))?),
)*
}
}
diff --git nitrocli/src/args.rs nitrocli/src/args.rs
index a70e816..694e1b 100644
--- nitrocli/src/args.rs
+++ nitrocli/src/args.rs
@@ -130,10 +130,6 @@ pub struct ConfigArgs {
subcmd: ConfigCommand,
}
-/// Prints the Nitrokey configuration
-#[derive(Debug, PartialEq, structopt::StructOpt)]
-pub struct ConfigGetArgs {}
-
/// Changes the Nitrokey configuration
#[derive(Debug, PartialEq, structopt::StructOpt)]
pub struct ConfigSetArgs {
@@ -374,7 +370,7 @@ pub struct UnencryptedSetArgs {
}
Command! {ConfigCommand, [
- Get(ConfigGetArgs) => |ctx, _| commands::config_get(ctx),
+ Get => commands::config_get,
Set(ConfigSetArgs) => config_set,
]}
Thanks! I'll have a look tomorrow, I hope. |
That's only for the auto generated |
Yes, but as this is the only place where the messages are used, I think it’s even more important to optimize them for this output (i. e. make them consistent). |
What I meant is:
The rest we have full control over. Correct? I don't want two sentences emitted by a random crate dictate how we formulate the rest of our help texts. |
Are you okay with me changing it? Then I'll adjust your changes and then merge. The rest looks great to me. |
Basically yes, there’s also a
Yes, if you prefer that then go ahead. |
I was under the impression that you also preferred that.
Or did I misunderstand? My worry is that since we both write in that style usually, there will be messages that end up in it. So from that perspective going with what |
Or did I misunderstand? My worry is that since we both write in that style usually, there will be messages that end up in it. So from that perspective going with what `structopt`/`clap` does just because it does that seems like the worse choice. I don't feel super strongly, so if you disagree we'll just keep what you have.
Well, I always have to check the existing code anyway if I haven’t
worked on a project recently. For the nitrokey crate, for example, I
try to use third person in the doc comments as it is recommended by the
Rust style guidelines [0].
Anyway, I don’t really care as long as we are consistent.
[0] https://doc.rust-lang.org/1.0.0/style/style/comments.html#sentence-structure
|
I'll leave it as-is. Let's see how it goes. |
Merged #97. Let's give this some bake time before doing a new release. Would you mind sharing your findings around the shell completion, @robinkrahl ? You mentioned there were complications. |
So far I only looked at the documentation. In their example, they use the |
Ah, thanks. It seems as if no |
Forgot that we already have an issue for shell completion support: #57 |
The author of
argparse
has made it fairly clear that the crate is in maintenance mode (at best; at worst I'd say it's abandoned). While it does pretty much what we want it to, there are flaws that I find increasingly annoying:So at this point I would be interested in considering alternatives. The only viable one I am aware of is
structopt
. In the past we've shied away from it due to bloat (without having conducted actual testing on this crate), and while that is still a concern, I'd say point 2) above tipped me over as user experience over all is still higher ranked.We should start by prototyping use of
structopt
and having a look at the outcome (code structure and resulting binary size).The text was updated successfully, but these errors were encountered: