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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! Define the CLI of the program, return the matched arguments

use clap::{
    crate_authors, crate_description, crate_name, crate_version, AppSettings, Arg, ArgMatches,
};
use std::{ffi::OsString, path::Path};

use crate::orbit::calc::models::MODELS;
use crate::{orbit::Results, F, I};

/// Define the `model` argument
fn model() -> Arg<'static, 'static> {
    Arg::with_name("model")
        .help("Set the model by index")
        .long("model")
        .takes_value(true)
        .empty_values(false)
        .required(true)
        .validator_os(|s| {
            s.to_str().map_or(
                Err(OsString::from("Argument isn't a valid UTF-8 string.")),
                |sl| {
                    if sl.chars().all(|c| c.is_numeric() || c == '-') {
                        sl.parse::<I>().map_or(
                            Err(OsString::from("Argument isn't a positive integer.")),
                            |i| {
                                if i == 0 {
                                    Err(OsString::from("Argument isn't a positive integer."))
                                } else if i > MODELS.len() {
                                    Err(OsString::from("There is no model with this index."))
                                } else {
                                    Ok(())
                                }
                            },
                        )
                    } else {
                        Err(OsString::from("Argument isn't numeric."))
                    }
                },
            )
        })
}

/// Define the `n` argument
fn n() -> Arg<'static, 'static> {
    Arg::with_name("n")
        .help("Set the number of iterations")
        .short("n")
        .long("iterations")
        .takes_value(true)
        .empty_values(false)
        .required(true)
        .validator_os(|s| {
            s.to_str().map_or(
                Err(OsString::from("Argument isn't a valid UTF-8 string.")),
                |sl| {
                    if sl.chars().all(|c| c.is_numeric() || c == '-') {
                        sl.parse::<I>().map_or(
                            Err(OsString::from("Argument isn't a positive integer.")),
                            |i| {
                                if i == 0 {
                                    Err(OsString::from("Argument isn't a positive integer."))
                                } else {
                                    Ok(())
                                }
                            },
                        )
                    } else {
                        Err(OsString::from("Argument isn't numeric."))
                    }
                },
            )
        })
}

/// Define the `h` argument
fn h() -> Arg<'static, 'static> {
    Arg::with_name("h")
        .help("Set the time step (in Myr)")
        .short("h")
        .long("step")
        .takes_value(true)
        .empty_values(false)
        .required(true)
        .validator_os(|s| {
            s.to_str().map_or(
                Err(OsString::from("Argument isn't a valid UTF-8 string.")),
                |sl| {
                    if sl
                        .chars()
                        .all(|c| c.is_numeric() || c == '-' || c == '.' || c == 'e' || c == 'E')
                    {
                        if sl.parse::<F>().is_ok() {
                            Ok(())
                        } else {
                            Err(OsString::from("Argument isn't a float number."))
                        }
                    } else {
                        Err(OsString::from("Argument isn't numeric."))
                    }
                },
            )
        })
}

/// Define the `s` argument
fn s() -> Arg<'static, 'static> {
    Arg::with_name("s")
        .help("[SM] Set the number of Monte Carlo simulations")
        .short("s")
        .long("simulations")
        .takes_value(true)
        .empty_values(false)
        .requires("sim")
        .validator_os(|s| {
            s.to_str().map_or(
                Err(OsString::from("Argument isn't a valid UTF-8 string.")),
                |sl| {
                    if sl.chars().all(|c| c.is_numeric() || c == '-') {
                        sl.parse::<I>().map_or(
                            Err(OsString::from("Argument isn't a positive integer.")),
                            |i| {
                                if i == 0 {
                                    Err(OsString::from("Argument isn't a positive integer."))
                                } else {
                                    Ok(())
                                }
                            },
                        )
                    } else {
                        Err(OsString::from("Argument isn't numeric."))
                    }
                },
            )
        })
}

/// Define the `output` argument
fn output() -> Arg<'static, 'static> {
    Arg::with_name("output")
        .help("Set the output directory to use")
        .short("o")
        .long("output")
        .takes_value(true)
        .empty_values(false)
        .required(true)
        .validator_os(|s| {
            if Path::new(s).is_dir() {
                Ok(())
            } else {
                Err(OsString::from(format!("\nNo such dir {:#?}.", s)))
            }
        })
}

// Define the `fields` argument
fn fields() -> Arg<'static, 'static> {
    Arg::with_name("fields")
        .help("Set the fields to write")
        .short("f")
        .long("fields")
        .takes_value(true)
        .empty_values(false)
        .required(true)
        .require_delimiter(true)
        .possible_values(Results::fields())
        .hide_possible_values(true)
}

/// Define the `file(s)` argument
fn files() -> Arg<'static, 'static> {
    Arg::with_name("file(s)")
        .help("Set the input file(s) to use")
        .multiple(true)
        .required(true)
        .validator_os(|s| {
            if Path::new(s).is_file() {
                Ok(())
            } else {
                Err(OsString::from(format!("\nNo such file {:#?}.", s)))
            }
        })
}

/// Define the `rev` argument
fn rev() -> Arg<'static, 'static> {
    Arg::with_name("rev")
        .help("Enable reverse mode [RM]")
        .long("reverse")
        .conflicts_with("sim")
}

/// Define the `sim` argument
fn sim() -> Arg<'static, 'static> {
    Arg::with_name("sim")
        .help("Enable simulation mode [SM]")
        .long("simulate")
        .requires("s")
}

/// Define the CLI of the program, return the matched arguments
#[must_use]
pub fn get() -> ArgMatches<'static> {
    clap::app_from_crate!()
        .help_message("Print help information")
        .version_message("Print version information")
        .after_help(
            "Documentation:\n\
             - https://codeberg.org/paveloom-university/Stellar-Astronomy-Laboratory-Workshop-S09-2021/src/branch/main/README.md\n\
             - https://github.com/paveloom-university/Stellar-Astronomy-Laboratory-Workshop-S09-2021/blob/main/README.md\n\
             - https://gitlab.com/paveloom-g/university/s09-2021/stellar-astronomy-laboratory-workshop/-/blob/main/README.md\n\
             - https://git.sr.ht/~paveloom/Stellar-Astronomy-Laboratory-Workshop-S09-2021/tree/main/item/README.md\n\
             \n\
             Reference:\n\
             - https://paveloom-university.github.io/Stellar-Astronomy-Laboratory-Workshop-S09-2021\n\
             - https://paveloom-g.gitlab.io/university/s09-2021/stellar-astronomy-laboratory-workshop",
        )
        .args(&[
            model(),
            n(),
            h(),
            s(),
            output(),
            fields(),
            files(),
            rev(),
            sim(),
        ])
        .settings(&[
            AppSettings::AllowNegativeNumbers,
            AppSettings::ArgRequiredElseHelp,
            AppSettings::TrailingVarArg,
        ])
        .get_matches()
}