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

naming conventions for separating cli apps from libs #56

Open
softprops opened this issue Oct 9, 2018 · 10 comments
Open

naming conventions for separating cli apps from libs #56

softprops opened this issue Oct 9, 2018 · 10 comments

Comments

@softprops
Copy link

I was going to start a discussion on this on twitter https://twitter.com/softprops/status/1049729377699794944 but then I remembered this working group existed.

What I'm looking for is some guidance and direction for the current community standards ( if they exist ) around what to name my workspace members when I want to split a lib from cli app that is an interface for the lib. Ideally I'd be able to share the name for both but that's not possible with separate packages because their names are their unique identifiers!

@killercup
Copy link
Contributor

Very good point! We should definintely add this to the book. Not sure where exactly; my first idea was to have the last chapter of the tutorial be "Growing your codebase".

Personally, I'd go for something like what I did with waltz (surprise!) but there are some alternatives. Here are some approaches I know of:

With the latter approach you can still have dependencies that only get included when you compile a certain binary. Or, actually, you can define that a certain binary will only get build if you enable some Cargo.toml features that also add some dependencies. This is what cargo-edit does.

@killercup
Copy link
Contributor

Regarding naming conventions, a look at crates.io shows that (.+)[-_]cli$ is pretty popular. That may be biased on my searches, though. Notable exception: grep-cli is a library for building grep-like CLI applications (extracted from ripgrep).

To have the default main.rs be called foo instead of foo_cli, you'll have to do something like this however.

@epage
Copy link
Contributor

epage commented Oct 9, 2018

Another relatively common one is -bin. Granted, I'm biased with cobalt-bin.
(Never knew crates.io accepted regexes)

  • -cli is more specific about what it is and can differentiate in case you have different cli, curses, gtk, qt, etc
  • -bin is a recommendation that can apply in any situation and matches with the target type in Cargo.

@softprops
Copy link
Author

I went with lib/cli but now that bin was mentioned I kind of like that better.

Another question of convention is the package folders name of the crate. I just used the short form not knowing what a good convention was but kept standardish names in cargo.toml files.

In any case I'm happy to see a discussion around this happening.if anything shakes out around the convention I'd recommend porting the "rust book" workspace example to use it. That was my first introduction to cargo workspaces and likely the will be the first for others so it seems like a good opportunity for "teaching" a convention through first example.

@killercup
Copy link
Contributor

killercup commented Oct 10, 2018 via email

@killercup
Copy link
Contributor

killercup commented Oct 10, 2018

From twitter thread: We should also have conventions around subcommands.

One last gotcha related to the naming of the workspace package. It affects the cargo install experience. cargo install emoji-clock-cli installs the emojiclock bin. I'm guessing this is where conventions will really come in handy to make this experience consistent for users
@softprops

Ohhh I just noticed we should also probably talk about subcommands: Where foo-cli installs foo, foo-bar, and foo-baz (which can be called using "foo", "foo bar", and "foo baz")
@killercup

@dwijnand
Copy link

👍 for -cli. If you create a CLI-only tool, and later decide you'd rather it have a GTK interface instead, your users would probaby prefer if you retired your fooify-cli and recommending your new fooify-desktop rather than your fooify-bin suddenly having a completely different interface in a new version.

@softprops
Copy link
Author

For the non cli/bin crate I'd like to claim a preference for keeping an unsuffixed/prefixed name. I've seen "foo-core" and "lib-foo" on the past. When I do imports I just want "use foo::Bar;"

@epage
Copy link
Contributor

epage commented Oct 10, 2018

Agreed but to be clear, there are time for a -core crate but it should generally be a subordinate of a lib crate that re-exports it imo

An example is predicates where I wanted the traits kept separate so I could break compatibility more freely on predicates without disrupting the crates that use the traits in their API.

@elasticdog
Copy link

I like the "two crates in one repo that is a cargo workspace" pattern with -cli for the specificity that @killercup mentioned. It seems like subcommands could naturally fall under the the -cli crate with multiple binary targets?

@epage epage transferred this issue from rust-cli/team Jul 19, 2021
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

5 participants