1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * Copyright Nate Lust 2018*/

#[doc(no_inline)]
pub use clap::ArgMatches;
use clap::{App, Arg, SubCommand};

/**
 * Builds and returns the sub command struct, containing all the options for the setup command
*/
pub fn build_setup<'a, 'b>() -> App<'a, 'b> {
    return SubCommand::with_name("setup")
        .arg(Arg::with_name("product").help("Product to setup").index(1))
        .arg(
            Arg::with_name("just")
                .help("ignore dependncies")
                .short("j")
                .long("just"),
        )
        .arg(
            Arg::with_name("relative")
                .help("setup relative path")
                .short("r")
                .long("relative")
                .takes_value(true),
        )
        .arg(
            Arg::with_name("keep")
                .help("keep exsisting setup products")
                .short("k")
                .long("keep"),
        )
        .arg(
            Arg::with_name("tag")
                .help("specify one or more tags to look up for products, evaluated left to right")
                .short("t")
                .long("tag")
                .multiple(true)
                .number_of_values(1)
                .takes_value(true),
        )
        .arg(
            Arg::with_name("inexact")
                .help("Run setup with Inexact versions as specified in the table files")
                .short("E")
                .long("inexact"),
        )
        .arg(
            Arg::with_name("verbose")
                .short("v")
                .long("verbose")
                .multiple(true)
                .help("Sets the level of verbosity, multiple occurances increases verbosity"),
        );
}

/**
 * Builds and returns the sub command struct, containing all the options for the list command
 */
fn build_list<'a, 'b>() -> App<'a, 'b> {
    return SubCommand::with_name("list")
                          .arg(Arg::with_name("product")
                               .help("Name of product to list (optional)")
                               .index(1)
                               .conflicts_with_all(&["setup", "local"]))
                          .arg(Arg::with_name("setup")
                               .help("List only setup products")
                               .short("s")
                               .long("setup")
                               .conflicts_with_all(&["product", "local"]))
                          .arg(Arg::with_name("tags")
                               .help("List only these tags (does not include current)")
                               .short("t")
                               .long("tags")
                               .multiple(true)
                               .number_of_values(1)
                               .takes_value(true))
                          .arg(Arg::with_name("onlyTags")
                               .help("Only list product & tags (faster than tags and versions, but does not indicate setup tag)")
                               .long("onlyTags"))
                          .arg(Arg::with_name("onlyVers")
                               .help("Only list product & versions (faster than tags and versions)")
                               .long("onlyVers")
                               .conflicts_with("onlyTags"))
                          .arg(Arg::with_name("local")
                               .help("Only list products that are setup as local products")
                               .short("l")
                               .long("local")
                               .conflicts_with_all(&["product", "setup"]));
}

/**
 * Builds and returns the sub command struct, containing all the options for the completions command.
 */
fn build_completions<'a, 'b>() -> App<'a, 'b> {
    return SubCommand::with_name("completions")
        .about("Creates auto completeion scripts for given shell")
        .arg(
            Arg::with_name("shell")
                .required(true)
                .possible_values(&["bash", "fish", "zsh", "elvish"])
                .help("Shell to create completions for"),
        );
}

/**
 * Builds the completetions for the sub command env. This allows the reups commands run in one
 * shell to be recored and replayed in another shell
 */
fn build_env<'a, 'b>() -> App<'a, 'b> {
    return SubCommand::with_name("env")
        .about("Save or restore an existing reups environment")
        .arg(
            Arg::with_name("command")
                .required(true)
                .possible_values(&["save", "restore", "delete", "list"])
                .help("Action to take for a given environment, to restore you most likely want to use the rrestore shell function"),
        )
        .arg(
            Arg::with_name("name")
                .required(false)
                .help("Optional name to save/restore"),
        )
        .arg(
            Arg::with_name("verbose")
                .short("v")
                .long("verbose")
                .multiple(true)
                .help("Sets the level of verbosity, multiple occurances increases verbosity"),
        );
}

/**
 * Builds and returns the sub command struct, containing all the options for the prep command.
 *
 * Currently this is basically an empty command just to create the setup option, but in the future
 * this command may take optional arguments such as a configuration file to use in preping.
 */
fn build_prep<'a, 'b>() -> App<'a, 'b> {
    return SubCommand::with_name("prep");
}

/**
 * This function is responsible for creating all the possible command line options and arguments for the main program, and each of the sub commands.
 */
pub fn build_cli() -> App<'static, 'static> {
    App::new("Rust Eups")
        .author("Nate Lust")
        .about("Dynamic environment management")
        .version(clap::crate_version!())
        .subcommand(build_setup())
        .subcommand(build_prep())
        .subcommand(build_list())
        .subcommand(build_completions())
        .subcommand(build_env())
}

/**
 * This is the main argument parser for the reups program. It parses the arguments from the command
 * line into a `ArgMatches` object containing all the supplied options.
 */
pub fn parse_args<'a>() -> ArgMatches<'a> {
    let matches = build_cli().get_matches();
    return matches;
}