pub fn generate_to<G, S, T>(app: &mut clap::App, bin_name: S, out_dir: T)
where
    G: Generator,
    S: Into<String>,
    T: Into<OsString>,
{
    let out_dir = PathBuf::from(out_dir.into());
    let file_name = G::file_name(app.get_bin_name().unwrap());

    let mut file = match File::create(out_dir.join(file_name)) {
        Err(why) => panic!("couldn't create completion file: {}", why),
        Ok(file) => file,
    };

    generate::<G, S>(app, bin_name, &mut file)
}
/// Generate a file for a specified generator at compile time.
///
/// **NOTE:** to generate the file at compile time you must use a `build.rs` "Build Script"
///
/// # Examples
///
/// The following example generates a bash completion script via a `build.rs` script. In this
/// simple example, we'll demo a very small application with only a single subcommand and two
/// args. Real applications could be many multiple levels deep in subcommands, and have tens or
/// potentially hundreds of arguments.
///
/// First, it helps if we separate out our `App` definition into a separate file. Whether you
/// do this as a function, or bare App definition is a matter of personal preference.
///
/// ```
/// // src/cli.rs
///
/// use clap::{App, Arg};
///
/// pub fn build_cli() -> App<'static> {
///     App::new("compl")
///         .about("Tests completions")
///         .arg(Arg::with_name("file")
///             .help("some input file"))
///         .subcommand(App::new("test")
///             .about("tests things")
///             .arg(Arg::with_name("case")
///                 .long("case")
///                 .takes_value(true)
///                 .help("the case to test")))
/// }
/// ```
///
/// In our regular code, we can simply call this `build_cli()` function, then call
/// `get_matches()`, or any of the other normal methods directly after. For example:
///
/// ```ignore
/// // src/main.rs
///
/// mod cli;
///
/// fn main() {
///     let m = cli::build_cli().get_matches();
///
///     // normal logic continues...
/// }
/// ```
///
/// Next, we set up our `Cargo.toml` to use a `build.rs` build script.
///
/// ```toml
/// # Cargo.toml
/// build = "build.rs"
///
/// [build-dependencies]
/// clap = "*"
/// ```
///
/// Next, we place a `build.rs` in our project root.
///
/// ```ignore
/// use clap_generate::{generate_to, generators::Bash};
///
/// include!("src/cli.rs");
///
/// fn main() {
///     let outdir = match env::var_os("OUT_DIR") {
///         None => return,
///         Some(outdir) => outdir,
///     };
///
///     let mut app = build_cli();
///     generate_to::<Bash, _, _>(
///         &mut app,     // We need to specify what generator to use
///         "myapp",      // We need to specify the bin name manually
///         outdir,       // We need to specify where to write to
///     );
/// }
/// ```
///
/// Now, once we compile there will be a `{bin_name}.bash` file in the directory.
/// Assuming we compiled with debug mode, it would be somewhere similar to
/// `<project>/target/debug/build/myapp-<hash>/out/myapp.bash`.
///
/// **NOTE:** Please look at the individual [generators](./generators/index.html)
/// to see the name of the files generated.
Generate a file for a specified generator at compile time.
NOTE:** to generate the file at compile time you must use a `build.rs` "Build Script"
Generate a file for a specified generator at compile time . NOTE : ** to generate the file at compile time you must use a ` build . rs ` " Build Script "
pub fn generate_to<G, S, T>(app: &mut clap::App, bin_name: S, out_dir: T)
where
    G: Generator,
    S: Into<String>,
    T: Into<OsString>,
{
    let out_dir = PathBuf::from(out_dir.into());
    let file_name = G::file_name(app.get_bin_name().unwrap());
    let mut file = match File::create(out_dir.join(file_name)) {
        Err(why) => panic!("couldn't create completion file: {}", why),
        Ok(file) => file,
    };
    generate::<G, S>(app, bin_name, &mut file)
}
pub fn generate_to < G , S , T > ( app : & mut clap :: App , bin_name : S , out_dir : T ) where G : Generator , S : Into < String > , T : Into < OsString > , { let out_dir = PathBuf :: from ( out_dir . into ( ) ) ; let file_name = G :: file_name ( app . get_bin_name ( ) . unwrap ( ) ) ; let mut file = match File :: create ( out_dir . join ( file_name ) ) { Err ( why ) => panic ! ( " " why ) , Ok ( file ) => file , } ; generate :: < G , S > ( app , bin_name , & mut file ) }
Generate a file for a specified generator at compile time.
Generate a file for a specified generator at compile time .
app
&mut clap::App
bin_name
S
out_dir
T
app
&mut clap::App
bin_name
S
out_dir
T
examples
The following example generates a bash completion script via a `build.rs` script. In this
simple example, we'll demo a very small application with only a single subcommand and two
args. Real applications could be many multiple levels deep in subcommands, and have tens or
potentially hundreds of arguments.

First, it helps if we separate out our `App` definition into a separate file. Whether you
do this as a function, or bare App definition is a matter of personal preference.



use clap::{App, Arg}.

pub fn build_cli() -> App<'static> {
App::new("compl")
.about("Tests completions")
.arg(Arg::with_name("file")
.help("some input file"))
.subcommand(App::new("test")
.about("tests things")
.arg(Arg::with_name("case")
.long("case")
.takes_value(true)
.help("the case to test")))
}
```

In our regular code, we can simply call this `build_cli()` function, then call
`get_matches()`, or any of the other normal methods directly after. For example.

```ignore
src/main.rs

mod cli.

fn main() {
let m = cli::build_cli().get_matches().

normal logic continues
}
```

Next, we set up our `Cargo.toml` to use a `build.rs` build script.


The following example generates a bash completion script via a ` build . rs ` script . In this simple example we ' ll demo a very small application with only a single subcommand and two args . Real applications could be many multiple levels deep in subcommands and have tens or potentially hundreds of arguments . First it helps if we separate out our ` App ` definition into a separate file . Whether you do this as a function or bare App definition is a matter of personal preference . use clap :: { App Arg } . pub fn build_cli () - > App< ' static > { App :: new ( " compl " ) . about ( " Tests completions " ) . arg ( Arg :: with_name ( " file " ) . help ( " some input file " )) . subcommand ( App :: new ( " test " ) . about ( " tests things " ) . arg ( Arg :: with_name ( " case " ) . long ( " case " ) . takes_value ( true ) . help ( " the case to test " ))) } ` ` ` In our regular code we can simply call this ` build_cli () ` function then call ` get_matches () ` or any of the other normal methods directly after . For example . ` ` ` ignore src / main . rs mod cli . fn main () { let m = cli :: build_cli () . get_matches () . normal logic continues } ` ` ` Next we set up our ` Cargo . toml ` to use a ` build . rs ` build script .
cargo.toml
build = "build.rs"

[build-dependencies]
clap = "*"
```

Next, we place a `build.rs` in our project root.

```ignore
use clap_generate::{generate_to, generators::Bash}.

include!("src/cli.rs").

fn main() {
let outdir = match env::var_os("OUT_DIR") {
None => return,
Some(outdir) => outdir,
}.

let mut app = build_cli();
generate_to::(
&mut app,     // We need to specify what generator to use
"myapp",      // We need to specify the bin name manually
outdir,       // We need to specify where to write to
);
}
```

Now, once we compile there will be a `{bin_name}.bash` file in the directory.
Assuming we compiled with debug mode, it would be somewhere similar to
`/target/debug/build/myapp-/out/myapp.bash`.

Please look at the individual [generators](./generators/index.html)
to see the name of the files generated.
build = " build . rs " [ build - dependencies ] clap = " * " ` ` ` Next we place a ` build . rs ` in our project root . ` ` ` ignore use clap_generate :: { generate_to generators :: Bash } . include! ( " src / cli . rs " ) . fn main () { let outdir = match env :: var_os ( " OUT_DIR " ) { None = > return Some ( outdir ) = > outdir } . let mut app = build_cli () ; generate_to :: ( &mut app // We need to specify what generator to use " myapp " // We need to specify the bin name manually outdir // We need to specify where to write to ) ; } ` ` ` Now once we compile there will be a ` { bin_name } . bash ` file in the directory . Assuming we compiled with debug mode it would be somewhere similar to ` / target / debug / build / myapp - / out / myapp . bash ` . Please look at the individual [ generators ] ( . / generators / index . html ) to see the name of the files generated .