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

Automated way to create an alias for cargo subcommand? #9061

Open
k0pernicus opened this issue Jan 10, 2021 · 5 comments
Open

Automated way to create an alias for cargo subcommand? #9061

k0pernicus opened this issue Jan 10, 2021 · 5 comments
Labels
A-aliases Area: command aliases C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-install S-needs-team-input Status: Needs input from team on whether/how to proceed.

Comments

@k0pernicus
Copy link
Contributor

Describe the problem you are trying to solve

I am trying to create an alias for cargo-generate.
The crate cargo-generate is automatically used when executing cargo generate.
With the help of structopt, we created an alias called gen to execute cargo-generate using cargo run -- gen instead of cargo run -- generate.

After installing the crate locally (cargo install --path .), the current way to execute cargo generate is to let cargo make an invocation of an external tool cargo-${command}, so cargo gen is an unknown command to cargo.

Describe the solution you'd like

A solution to create aliases is to generate a file called config inside $HOME/.cargo, and fill it with an alias, like this:

#[alias]
gen = "generate"

but this method needs a manual intervention from the user.

I would like to know if it exists an "automated" way to create this alias easily after installing the crate on a system, or not.

Notes

Original issue, posted in cargo-generate, here.

Thanks a lot!

@k0pernicus k0pernicus added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Jan 10, 2021
@CPerezz
Copy link
Contributor

CPerezz commented Feb 3, 2021

You actually have the capability of stating these aliases. See the docs.

I'm not sure if I really understand the details of your issue but what you're trying to do may be doable by adding to the build.rs of your cargo command crate some code that sets this alias in .cargo.toml for example.

A better option anyway is to maybe display some message suggesting to add the alias to the config. Since users might have it symlinked or many other stuff that you might want to avoid dealing with.

I'm not sure wether this can be done on any other way.

@ehuss ehuss added A-aliases Area: command aliases Command-install labels Feb 13, 2021
@rnag
Copy link

rnag commented Apr 8, 2022

there's minor problems with both of the posts above. for example, #[alias] isn't picked up by cargo - looks like removing # works; for the second post, the link just leads back to this same page. there should be a link to docs on alias somewhere on the web though, but it's a pain because the link provided here actually doesn't lead anywhere.

@josecelano
Copy link

there's minor problems with both of the posts above. for example, #[alias] isn't picked up by cargo - looks like removing # works; for the second post, the link just leads back to this same page. there should be a link to docs on alias somewhere on the web though, but it's a pain because the link provided here actually doesn't lead anywhere.

This seems to be the right link:

https://doc.rust-lang.org/cargo/reference/config.html#alias

But it does not work for me with or without the #:

#[alias]
cov = "llvm-cov --lcov --output-path=./coverage/lcov.info"
[alias]
cov = "llvm-cov --lcov --output-path=./coverage/lcov.info"

@weihanglo
Copy link
Member

@josecelano, if you're talking about how aliases work in genernal, that should just work when the configuration file is in one of the paths of configuration discovery, and have a cargo-llvm-cov binary under your $PATH.

@josecelano
Copy link

josecelano commented Mar 2, 2023

Thank you @weihanglo. It works now using ./cargo/config.toml. I thought I could use the ./config.toml in the root dir.

@weihanglo weihanglo added the S-needs-team-input Status: Needs input from team on whether/how to proceed. label May 24, 2023
Offroaders123 added a commit to Offroaders123/rust_nbt that referenced this issue Dec 18, 2023
This demo file was originally from a demo I made back on July 30. I figured out how to get a sort of 'npm run dev' kind of thing working, I really like it! This kind of quick-feedback programming is how I get things done fast, as I can easily see when things go right, or wrong.

https://doc.rust-lang.org/rust-by-example/custom_types/structs.html
https://doc.rust-lang.org/book/ch16-01-threads.html
https://www.reddit.com/r/rust/comments/9twt89/show_rrust_cargocmd_like_npm_scripts_but_for_cargo/ (I was curious if you could do something like `cargo run dev` for this kind of hot reload script, but it didn't seem to work from what I tried. It is possible it sounds like, but I don't think I'm implementing it right. Going to use a Bash script for the meantime. I want it to work cross-platform though, so you can use this on Windows too and not have to do anything special for that. I know I could make a Batch file, but I'd rather use simple tooling with Cargo than roll my own thing for that. That's the same case with npm as well, to use the tools that are available to you! :D)
https://doc.rust-lang.org/cargo/reference/config.html#alias
https://pseitz.github.io/toml-to-json-online-converter/ (Neat concept, finding the similarities between TOML, JSON, and SNBT/NBT)
rust-lang/cargo#9061
rust-lang/cargo#8486 (comment) (Is my alias not working because of how I defined it in the config?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-aliases Area: command aliases C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-install S-needs-team-input Status: Needs input from team on whether/how to proceed.
Projects
None yet
Development

No branches or pull requests

6 participants