Skip to content

Commit

Permalink
Added shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
StandingPadAnimations committed Jul 23, 2023
1 parent 7729478 commit edb9105
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ edition = "2021"

[dependencies]
clap = { version = "4.3.19", features = ["derive"] }
clap_complete = "4.3.2"
serde = { version = "1.0.174", features = ["derive"] }
serde_yaml = "0.9.25"
46 changes: 30 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
use std::{collections::HashMap, path::PathBuf};
use clap::Parser;
use clap::{Parser, ValueHint, Command, CommandFactory};
use clap_complete::{Shell, Generator, generate};
use serde::{Serialize, Deserialize};

/// Arguments for bpy-build
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to file
#[arg(short, long)]
#[arg(short, long, value_hint = ValueHint::FilePath)]
path: Option<PathBuf>,

/// Creates shell completion script
#[arg(long = "generate", value_enum)]
generator: Option<Shell>,
}


/// Create completion script
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}

/// Configuration found in bpy-build.yaml
Expand All @@ -27,19 +38,22 @@ struct BpyBuildConf {

fn main() -> Result<(), serde_yaml::Error> {
let args = Args::parse();

let config: BpyBuildConf = match args.path {
Some(path) => {
let file = std::fs::read_to_string(path).expect("File not found !");
println!("{}", file);
serde_yaml::from_str(&file).unwrap()
}
None => {
let file = std::fs::read_to_string("./bpy-build.yaml").expect("File not found !");
println!("{}", file);
serde_yaml::from_str(&file).unwrap()
}
};
println!("{:?}", config);
if let Some(shell) = args.generator {
print_completions(shell, &mut Args::command())
} else {
let config: BpyBuildConf = match args.path {
Some(path) => {
let file = std::fs::read_to_string(path).expect("File not found !");
println!("{}", file);
serde_yaml::from_str(&file).unwrap()
}
None => {
let file = std::fs::read_to_string("./bpy-build.yaml").expect("File not found !");
println!("{}", file);
serde_yaml::from_str(&file).unwrap()
}
};
println!("{:?}", config);
}
Ok(())
}

0 comments on commit edb9105

Please sign in to comment.