Skip to content

Commit be3ce2a

Browse files
authored
Merge pull request #18 from flatcar/better-clap
Improve documentation while making better use of clap
2 parents ad3b888 + 60e7c22 commit be3ce2a

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

Cargo.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2021"
1313
fs2 = "0.4"
1414
# Public dependencies, exposed through library API.
1515
error-chain = { version = "0.12", default-features = false }
16-
clap = { version = "4.4.6", features = ["cargo"] }
16+
clap = { version = "4.4.6", features = ["cargo", "string", "wrap_help"] }
1717
uzers = "0.11.3"
1818
openssh-keys = "0.6.2"
1919
lazy_static = "1.4.0"

src/main.rs

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern crate uzers;
2525
extern crate update_ssh_keys;
2626

2727
use clap::parser::ValueSource;
28-
use clap::{crate_version, Arg, Command};
28+
use clap::{crate_version, Arg, Command, ValueHint};
2929
use std::fs::File;
3030
use std::path::PathBuf;
3131
use update_ssh_keys::errors::*;
@@ -165,13 +165,9 @@ pub const USS_TEMPLATE: &str = "\
165165
{about-with-newline}";
166166

167167
pub const ABOUT_TEXT: &str = "\
168-
This tool provides a consistent way for different systems to add ssh public
169-
keys to a given user account, usually the default current user.
170-
If -a, -A, -d, nor -D are provided then the authorized_keys file is simply
171-
regenerated using the existing keys.
168+
This tool provides a consistent way for different systems to add SSH public keys to a given user account, usually the default current user. If -a, -A, -d, nor -D are provided, then the authorized_keys file is simply regenerated using the existing keys.
172169
173-
With the -a option keys may be provided as files on the command line. If no
174-
files are provided with the -a option the keys will be read from stdin.";
170+
When using the -a or -A options, keys must be provided as files on the command line, or if no files are given, via standard input.";
175171

176172
fn config() -> Result<Config> {
177173
// get the default user by figuring out the current user; if the current user
@@ -190,57 +186,81 @@ fn config() -> Result<Config> {
190186
.version(crate_version!())
191187
.help_template(USS_TEMPLATE)
192188
.about(ABOUT_TEXT)
193-
.arg(Arg::new("user").short('u').help(format!(
194-
"Update the given user's authorized_keys file. [{}]",
195-
default_user
196-
)))
189+
.arg(
190+
Arg::new("user")
191+
.short('u')
192+
.long("user")
193+
.value_name("USERNAME")
194+
.value_hint(ValueHint::Username)
195+
.default_value(default_user)
196+
.help("Update the given user's authorized_keys file"),
197+
)
197198
.arg(
198199
Arg::new("no-replace")
199200
.short('n')
200-
.num_args(0)
201-
.help("When adding, don't replace an existing key with the given name."),
201+
.long("no-replace")
202+
.action(clap::ArgAction::SetTrue)
203+
.help("When adding, don't replace an existing key with the given name"),
202204
)
203205
.arg(
204206
Arg::new("list")
205207
.short('l')
208+
.long("list")
206209
.num_args(0)
207-
.help("List the names and number of keys currently installed."),
210+
.help("List the names and number of keys currently installed"),
208211
)
209212
.arg(
210213
Arg::new("add")
211214
.short('a')
212-
.help("Add the given keys, using the given name to identify them."),
215+
.long("add")
216+
.value_name("IDENTIFIER")
217+
.help("Add keys from files or standard input under the given identifier"),
213218
)
214219
.arg(
215220
Arg::new("add-force")
216221
.short('A')
217-
.help("Add the given keys, even if it was disabled with '-D'."),
222+
.long("add-force")
223+
.value_name("IDENTIFIER")
224+
.help("Add keys, even if the given identifier was disabled with '-D'"),
218225
)
219226
.arg(
220227
Arg::new("delete")
221228
.short('d')
222-
.help("Delete keys identified by the given name."),
229+
.long("delete")
230+
.value_name("IDENTIFIER")
231+
.help("Delete keys stored under the given identifier"),
223232
)
224233
.arg(
225234
Arg::new("disable")
226235
.short('D')
227-
.help("Disable the given set from being added with '-a'."),
236+
.long("disable")
237+
.value_name("IDENTIFIER")
238+
.help("Delete keys and prevent further addition with '-a'"),
228239
)
229240
.arg(
230241
Arg::new("ssh_dir")
231242
.short('s')
232243
.long("ssh-dir")
233-
.help("location of the ssh configuration directory (defaults to ~/.ssh)"),
244+
.value_name("DIR")
245+
.value_hint(ValueHint::DirPath)
246+
.default_value("~/.ssh")
247+
.help("Location of the SSH configuration directory"),
248+
)
249+
.arg(
250+
Arg::new("keys")
251+
.value_name("KEYS")
252+
.num_args(1..)
253+
.value_hint(ValueHint::FilePath)
254+
.help("Key file paths"),
234255
)
235-
.arg(Arg::new("keys").num_args(1..).help("path to key files"))
236256
.get_matches();
237257

238258
let command = matches
239259
.get_one::<String>("add")
240260
.map(|name| UssCommand::Add {
241261
name: name.into(),
242262
force: false,
243-
replace: !matches.contains_id("no-replace"),
263+
replace: !matches.get_flag("no-replace"),
244264
stdin: !matches.contains_id("keys"),
245265
keyfiles: matches
246266
.get_many::<String>("keys")
@@ -253,7 +273,7 @@ fn config() -> Result<Config> {
253273
.map(|name| UssCommand::Add {
254274
name: name.into(),
255275
force: true,
256-
replace: !matches.contains_id("no-replace"),
276+
replace: !matches.get_flag("no-replace"),
257277
stdin: !matches.contains_id("keys"),
258278
keyfiles: matches
259279
.get_many::<String>("keys")
@@ -281,11 +301,13 @@ fn config() -> Result<Config> {
281301
UssCommand::Sync
282302
});
283303

284-
let user = matches
285-
.get_one::<String>("user")
286-
.map_or(default_user, String::from);
304+
let user = matches.get_one::<String>("user").unwrap().to_owned();
287305

288-
let ssh_dir = matches.get_one::<String>("ssh_dir").map(PathBuf::from);
306+
let ssh_dir = if matches.value_source("ssh_dir") == Some(ValueSource::DefaultValue) {
307+
None
308+
} else {
309+
matches.get_one::<String>("ssh_dir").map(PathBuf::from)
310+
};
289311

290312
Ok(Config {
291313
user,

0 commit comments

Comments
 (0)