#[derive(Parser)]
struct Cli {
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(clap::Subcommand)]
enum Subcommand {
Index {
#[clap(parse(from_os_str))]
path: path::PathBuf,
},
Show {
item: Option<String>,
},
Cd {
term: String,
},
List,
Init {
shell: InitShell,
},
Search {
term: Option<String>,
},
Add {
category: String,
title: String,
},
}
fn main(){
let cli = Cli::parse();
match cli.subcommand{
Subcommand::Index=>{/*code here*/}
Subcommand::Show=>{/*and here*/}
Subcommand::Display=>{/*and also here*/}
Subcommand::Cd=>{}
Subcommand::List=>{}
// ... more matches
}
}
When I run my program with --help, the subcommands section looks like this:
SUBCOMMANDS:
add
cd
help
index
init
list
search
show
I would like to define some aliases, such as ls for list or display for show, so that
the help looks something like this:
SUBCOMMANDS:
add
cd, path
help
index
init
list, ls
search
show, display
I can see that cargo does something like this, with cargo build equal to cargo b.
I looked at the clap documentation and was able to find an alias function for the builder api,
but I could not find how to do this with the derive api. Is is this possible, and if so, how can I do it?
The Clap derive docs say (under Possible Value Attributes):
Possible Value Attributes
These correspond to a [PossibleValue][crate::PossibleValue].
Raw attributes: Any [PossibleValue method][crate::PossibleValue] can also be used as an attribute, see
Terminology for syntax.
e.g. #[clap(alias("foo"))] would translate to pv.alias("foo")
You also say you wish the aliases to show up under help. In that case, you want to use visible_alias instead:
#[clap(visible_alias("foo"))]
Related
I am working on a node.js project and have created pseudo-enum elements using an object literal as below:
export const Relation = { OTHER: '', FRIEND: 'fr', ENEMY: 'en' }
In my code, I am using the enum elements like this:
if (player.relation === Relation.ENEMY)
I am using webpack for compiling my code and I would like to know how I can compile the above code to get something like this:
if (player.relation === 'en')
If there is no way to do that, I would like to not compile unused enum fields.
I would appreciate if someone could guide me on how to achieve this.
This is my data model:
pub struct RaffleDetails {
prize: Balance,
start: Timestamp,
end: Timestamp,
participants: UnorderedMap<AccountId, Balance>,
}
pub struct RaffleDapp {
raffles: UnorderedMap<AccountId, RaffleDetails>,
}
How can I insert a key-value pair in the 'participants' variable?
I tried self.raffles.get(&raffle_account_id).unwrap().participants.insert(&env::predecessor_account_id(), &confidence); but it's not persistent.
References:
UnorderedMap
NEAR Rust SDK
You need to make sure you are updating the RaffleDetails instance that is in the map, not a copy/clone of it.
I'm not familiar with UnorderedMap, but it seems to me the get() method returns a copy of the value that is in the map, so you are only updating the copied value. I don't know if UnorderedMap allows you to mutate a value in it directly (skimming through the docs, I don't see such a method). What you can do though is re-insert the modified RaffleDetails into the raffles map (so as to replace the old one with the modified one).
I'm talking about something like this (I haven't tested compiling it):
let o = self.raffles.get(&raffle_account_id);
if let copied_rd = Some(o) {
copied_rd.participants.insert(&env::predecessor_account_id(), &confidence);
self.raffles.insert(&raffle_account_id, &copied_rd);
}
This question already has answers here:
Why can't I store a value and a reference to that value in the same struct?
(4 answers)
Closed 1 year ago.
imagine you have this two structs, one for creating things and one to store them.
struct Things {
name: String,
}
struct Backpack<'backpack> {
all_things: Option<Vec<Things>>,
favorite: Option<[&'backpack Things; 2]>,
}
all_things in Backpack are all the things that you are carrying around, but you can choose only two to be your favorite things.
in orther to add things to your backpack you run this code
let thing1 = Things {
name: String::from("pencil"),
};
let thing2 = Things {
name: String::from("notebook"),
};
let thing3 = Things {
name: String::from("phone"),
};
let mut my_backpack = Backpack {
all_things: None,
favorite: None,
};
my_backpack.all_things = Some(vec![thing1, thing2, thing3]);
you first initialize your backpack to be empty, and then add some items to your backpack.
The problem I have is to add items to favorite, because I only want a reference to that item from all_things.
my_backpack.favorite = Some([
&my_backpack.all_things.unwrap()[0],
&my_backpack.all_things.unwrap()[1],
])
whenever I do this, I got a compile error saying that the value has being moved.
How can I create an array of my favorite things with only references from all_things that are in my backpack. I don't want to copy them because they already exists on all_things.
Note that all_things and favorite can be empty, so that's why I use Option enum.
When you do my_backpack.all_things.unwrap(), you take the ownership of the value in the Option enum, thus the borrow checker error.
All you need to do is to reference the vector instead of taking the ownership:
my_backpack.favorite = Some([
&my_backpack.all_things.as_ref().unwrap()[0],
&my_backpack.all_things.as_ref().unwrap()[1],
]);
I have an each iteration in Puppet to install Perl module extensions:
$extensions_list = ["extension1",
"extension2",
]
$extensions_list.each |$extls| {
exec { $extls:
path => '/usr/local/bin/:/usr/bin/:/bin/',
command => "wget http://search.cpan.org/CPAN/authors/id/B/BP/BPS/extension1-1.00.tar.gz",
}
}
What I would like it to do as well is to take into account the version number, as in:
$extensions_list = ["extension1", "1.00",
"extension2", "2.00",
]
$extensions_list.each |$extls| {
exec { $extls:
path => '/usr/local/bin/:/usr/bin/:/bin/',
command => "wget http://search.cpan.org/CPAN/authors/id/B/BP/BPS/extension1-1.00.tar.gz",
}
}
So, I'd like it to be able to take the first two variables in the array to install the first extension and then the next two and install that and so on and so on as I add new extensions. That way I can just add the name and version number to my array and it will install them in turn.
I have an each iteration in Puppet to install Perl module extensions:
Well, no, not exactly. You have an each operator to declare an Exec resource corresponding to each element of your array. Among potentially important distinctions from what you said is that the operation is evaluated during catalog building, so no actual installations are taking place at that time.
So, I'd like it to be able to take the first two variables in the array to install the first extension and then the next two and install that and so on
You could use the slice() function to split the array into an array of two-element arrays, and iterate over that. Consider, however, how much more natural it would be to use a hash instead of an array as the underlying data structure. Example:
$extensions_hash = {"extension1" => "1.00",
"extension2" => "2.00",
}
$extensions_hash.each |$extls, $extv| {
exec { $extls:
path => '/usr/local/bin/:/usr/bin/:/bin/',
command => "wget http://search.cpan.org/CPAN/authors/id/B/BP/BPS/$extls-$extv.tar.gz",
}
}
Lets say I want to write a little client for an HTTP API. It has a resource that returns a list of cars:
GET /cars
It also accepts the two optional query parameters color and manufacturer, so I could query specific cars like:
GET /cars?color=black
GET /cars?manufacturer=BMW
GET /cars?color=green&manufacturer=VW
How would I expose these resources properly in Rust? Since Rust doesn't support overloading, defining multiple functions seems to be the usual approach, like:
fn get_cars() -> Cars
fn get_cars_by_color(color: Color) -> Cars
fn get_cars_by_manufacturer(manufacturer: Manufacturer) -> Cars
fn get_cars_by_manufacturer_and_color(manufacturer: Manufacturer, color: Color) -> Cars
But this will obviously not scale when you have more than a few parameters.
Another way would be to use a struct:
struct Parameters {
color: Option<Color>,
manufacturer: Option<Manufacturer>
}
fn get_cars(params: Parameters) -> Cars
This has the same scaling issue, every struct field must be set on creation (even if its value is just None).
I guess I could just accept a HashMap<String, String>, but that doesn't sound very good either.
So my question is, what is the proper/best way to do this in Rust?
You could use the Builder pattern, as mentioned here. For your particular API, it could look like this:
Cars::new_get()
.by_color("black")
.by_manufacturer("BMW")
.exec();
I would like to point out that no matter the solution, if you wish for a compile-time checked solution the "url parsing -> compile-time checkable" translation is necessarily hard-wired. You can generate that with an external script, with macros, etc... but in any case for the compiler to check it, it must exist at compile-time. There just is no short-cut.
Therefore, no matter which API you go for, at some point you will have something akin to:
fn parse_url(url: &str) -> Parameters {
let mut p: Parameters = { None, None };
if let Some(manufacturer) = extract("manufacturer", url) {
p.manufacturer = Some(Manufacturer::new(manufacturer));
}
if let Some(color) = extract("color", url) {
p.color = Some(Color::new(color));
}
p
}
And although you can try and sugarcoat it, the fundamentals won't change.