Skip to content

Commit

Permalink
feat: generate completion scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Yaroslav Bolyukin <[email protected]>
  • Loading branch information
CertainLach committed Mar 27, 2021
1 parent ee34bba commit 4744c15
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmds/jrsonnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ thiserror = "1.0"
[dependencies.clap]
git = "https://github.com/clap-rs/clap"
rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"

[dependencies.clap_generate]
git = "https://github.com/clap-rs/clap"
rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"
43 changes: 41 additions & 2 deletions cmds/jrsonnet/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::AppSettings;
use clap::Clap;
use clap::{AppSettings, Clap, IntoApp};
use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
use jrsonnet_evaluator::{error::LocError, EvaluationState, ManifestFormat};
use std::{
Expand All @@ -8,6 +7,7 @@ use std::{
io::Write,
path::PathBuf,
rc::Rc,
str::FromStr,
};

#[cfg(feature = "mimalloc")]
Expand All @@ -21,6 +21,29 @@ struct DebugOpts {
/// This shouldn't be changed unless jrsonnet is failing with stack overflow error.
#[clap(long, name = "size")]
pub os_stack: Option<usize>,
/// Generate completions script
#[clap(long)]
generate: Option<GenerateTarget>,
}

enum GenerateTarget {
Bash,
Zsh,
Fish,
PowerShell,
}
impl FromStr for GenerateTarget {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bash" => Ok(Self::Bash),
"zsh" => Ok(Self::Zsh),
"fish" => Ok(Self::Fish),
"powershell" => Ok(Self::PowerShell),
_ => Err("unknown target"),
}
}
}

#[derive(Clap)]
Expand All @@ -43,6 +66,22 @@ struct Opts {

fn main() {
let opts: Opts = Opts::parse();

if let Some(target) = opts.debug.generate {
use clap_generate::{generate, generators};
use GenerateTarget::*;
let app = &mut Opts::into_app();
let buf = &mut std::io::stdout();
let bin = "jrsonnet";
match target {
Bash => generate::<generators::Bash, _>(app, bin, buf),
Zsh => generate::<generators::Zsh, _>(app, bin, buf),
Fish => generate::<generators::Fish, _>(app, bin, buf),
PowerShell => generate::<generators::PowerShell, _>(app, bin, buf),
}
std::process::exit(0);
};

let success;
if let Some(size) = opts.debug.os_stack {
success = std::thread::Builder::new()
Expand Down

0 comments on commit 4744c15

Please sign in to comment.