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

feat: first iteration of cli #40

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

kristof-mattei
Copy link
Collaborator

@kristof-mattei kristof-mattei commented Jan 15, 2025

In response to #35

Hoping to get some initial conversation started here about how we want to design the CLI.

How close do we want to mirror the original one? clap does not support multi-character short args (like -ab).

@kristof-mattei kristof-mattei marked this pull request as draft January 15, 2025 00:25
@xamgore
Copy link
Collaborator

xamgore commented Jan 15, 2025

Hello, thank you so much!

How close do we want to mirror the original one? clap does not support multi-character short args (like -ab).

I wouldn't bother then. If unknown arguments (like -df) raise an error, it's good feedback that something going wrong.

yake/src/main.rs Outdated
struct Cli {
// -ti, --text_input TEXT Input text, SURROUNDED by single quotes(')
#[arg(conflicts_with = "input_file", long)]
text_input: String,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't it be Option<String> ?

If you don't provide text_input you get an error, however if input_file is provided text_input could, and should remains unprovided.

Similarly should input_file be Option<PathBuf> ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

cargo run -- --help yield " -i, --input-file <INPUT_FILE> " instead of -ti

-ti feel weird though. One letter flag -i is a better alternative despite the retro-compatibility

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm moving the inputs to a separate struct so we can use clap's required = true and multiple = false.

The new struct will have 2 Options

yake/src/main.rs Outdated

// -n, --ngram-size INTEGER Max size of the ngram.
#[arg(short, long)]
ngram_size: usize,
Copy link
Collaborator

Choose a reason for hiding this comment

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

ngram_size, dedup_function, dedup_lim and window_size are all optional in python yake cli and in this rust implementation thanks to Config::default.
Maybe default values could provide a much easier to use cli.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a great comment. And a point that I have struggled with when designing CLIs with clap.

Where do you put the defaults? The CLI is a single struct that is all the CLI inputs, but then gets transformed into other structs with their own defaults.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In other words:

when the user looks at the help of ngram_size, they expect it to be optional.

BUT... it does have a default, i.e. 3 in this codebase.

So do we expect the user to see this 3? If so we should do

#[arg(short, long, default_value_t = Config::default().ngrams)]
ngram_size: usize

That way the default is shown as

  -n, --ngram-size <NGRAM_SIZE>    Max size of the ngram [default: 3]

However, if we make it an Option<usize>:

  -n, --ngram-size <NGRAM_SIZE>    Max size of the ngram

The default isn't shown.

Copy link
Collaborator

@xamgore xamgore Jan 15, 2025

Choose a reason for hiding this comment

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

My cent: don't be scared to repeat the defaults, as in such case they will be visible to the user. Which is beautiful and allows tuning parameters. You may do something like this to ensure nobody will forget a field in the future:

impl From<ClapConfig> for Config {
  fn from(value: ClapConfig) -> Self {
    let ClapConfig { one, two, three } = value;  // if ClapConfig has a new field, it has to be added here

    // Config has a new field, it has to be added here
    Self { ... }
  }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@xamgore I've done something for which the CLI reads the defaults from yake's config, so any changes there propagate now.

@@ -58,6 +90,7 @@ contractions = "0.5.4"
segtok = "0.1.0"
levenshtein = "1.0.5"
indexmap = "2.7.0"
serde = "1.0.217"
Copy link
Collaborator

Choose a reason for hiding this comment

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

hi 😏, @Jeanbouvatt, could you put it under optional serde feature flag?

It's common for rust community to reduce the burden of dependencies. Like, if I don't need JSON at all, serde won't be compiled.

For that, a crate defines an optional feature, which is kinda synced with the dependency.

  • If the feature is manually listed yake-rust = { version = "...", features = ["ineedjson"] }
  • If the crate is listed as a dependency in user's Cargo.toml: [dependencies] serde = "*"

In any of those two cases, ineedjson will be turned on, and thus serde crate will be installed. Even more cfg!(feature = "ineedjson") will be true, and #[cfg(feature = "ineedjson")] macro will include the following code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure! will do

Copy link
Collaborator

Choose a reason for hiding this comment

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

Here it is

kristof-mattei#2

@xamgore
Copy link
Collaborator

xamgore commented Jan 16, 2025

Hey guys, if you feel like working with forked repository is a burden, @quesurifn can grant you permission for collaborating directly here.

@xamgore
Copy link
Collaborator

xamgore commented Jan 19, 2025

Ok, so we are basically waiting for kristof-mattei#3 being merged and ready to move on

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.

3 participants