git.delta.rocks / jrsonnet / refs/commits / 4744c15d089b

difftreelog

feat generate completion scripts

Yaroslav Bolyukin2021-03-27parent: #ee34bba.patch.diff
in: master

2 files changed

modifiedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
23git = "https://github.com/clap-rs/clap"23git = "https://github.com/clap-rs/clap"
24rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"24rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"
25
26[dependencies.clap_generate]
27git = "https://github.com/clap-rs/clap"
28rev = "52814b893c87e1c0350cae13fc1988fe2aa9886a"
2529
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
1use clap::AppSettings;1use clap::{AppSettings, Clap, IntoApp};
2use clap::Clap;
3use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};2use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
4use jrsonnet_evaluator::{error::LocError, EvaluationState, ManifestFormat};3use jrsonnet_evaluator::{error::LocError, EvaluationState, ManifestFormat};
5use std::{4use std::{
8 io::Write,7 io::Write,
9 path::PathBuf,8 path::PathBuf,
10 rc::Rc,9 rc::Rc,
10 str::FromStr,
11};11};
1212
13#[cfg(feature = "mimalloc")]13#[cfg(feature = "mimalloc")]
21 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.21 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.
22 #[clap(long, name = "size")]22 #[clap(long, name = "size")]
23 pub os_stack: Option<usize>,23 pub os_stack: Option<usize>,
24 /// Generate completions script
25 #[clap(long)]
26 generate: Option<GenerateTarget>,
24}27}
28
29enum GenerateTarget {
30 Bash,
31 Zsh,
32 Fish,
33 PowerShell,
34}
35impl FromStr for GenerateTarget {
36 type Err = &'static str;
37
38 fn from_str(s: &str) -> Result<Self, Self::Err> {
39 match s {
40 "bash" => Ok(Self::Bash),
41 "zsh" => Ok(Self::Zsh),
42 "fish" => Ok(Self::Fish),
43 "powershell" => Ok(Self::PowerShell),
44 _ => Err("unknown target"),
45 }
46 }
47}
2548
26#[derive(Clap)]49#[derive(Clap)]
27#[clap(50#[clap(
44fn main() {67fn main() {
45 let opts: Opts = Opts::parse();68 let opts: Opts = Opts::parse();
69
70 if let Some(target) = opts.debug.generate {
71 use clap_generate::{generate, generators};
72 use GenerateTarget::*;
73 let app = &mut Opts::into_app();
74 let buf = &mut std::io::stdout();
75 let bin = "jrsonnet";
76 match target {
77 Bash => generate::<generators::Bash, _>(app, bin, buf),
78 Zsh => generate::<generators::Zsh, _>(app, bin, buf),
79 Fish => generate::<generators::Fish, _>(app, bin, buf),
80 PowerShell => generate::<generators::PowerShell, _>(app, bin, buf),
81 }
82 std::process::exit(0);
83 };
84
46 let success;85 let success;
47 if let Some(size) = opts.debug.os_stack {86 if let Some(size) = opts.debug.os_stack {