Skip to content

Commit

Permalink
fixed issue with argument optionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-empty committed Apr 14, 2023
1 parent fe06623 commit 7cfaedd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shortcut"
version = "1.0.0"
version = "1.0.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
27 changes: 18 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ANSI_BOLD_OFF: &str = "\x1b[22m";
struct Cli {
/// The program name, e.g. `firefox`
#[arg()]
program_name: String,
program_name: Option<String>,

/// List all available shortcut pages in the cache
#[arg(short, long, action = clap::ArgAction::SetTrue)]
Expand All @@ -49,8 +49,6 @@ struct Cli {
fn main() {
let cli = Cli::parse();

//TODO, validate program_name
let program_name = cli.program_name.trim().to_owned();
let is_colour_on = !cli.no_colour.unwrap();
if cli.update.unwrap() {
match update() {
Expand All @@ -62,17 +60,28 @@ fn main() {
}
} else if cli.list.unwrap() {
list_shortcuts();
} else if program_name != "" {
get_shortcut_page(&program_name, is_colour_on);
} else {
eprintln!("No valid argument specified");
exit(1)
match cli.program_name.map(|s| s.trim().to_owned()) {
Some(program_name) if !program_name.is_empty() => {
// let program_name = program_name.trim().to_owned();
get_shortcut_page(&program_name, is_colour_on);
}
_ => {
eprintln!("No valid argument specified");
exit(1)
}
}
}
}

fn has_write_permission(path: PathBuf) -> Result<bool, std::io::Error> {
let metadata = fs::metadata(path)?;
Ok(!metadata.permissions().readonly())
match fs::create_dir_all(path.to_owned()) {
Ok(()) => {
let metadata = fs::metadata(path)?;
Ok(!metadata.permissions().readonly())
}
Err(error) => Err(error),
}
}

fn update() -> Result<()> {
Expand Down

0 comments on commit 7cfaedd

Please sign in to comment.