Skip to content

Commit e71b6f4

Browse files
committed
Move the clap command-line parsing into a separate file
1 parent 94c9b67 commit e71b6f4

File tree

2 files changed

+91
-88
lines changed

2 files changed

+91
-88
lines changed

src/cmd_line.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use clap::{
2+
crate_authors, crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgGroup,
3+
ArgMatches,
4+
};
5+
6+
pub fn get_args() -> ArgMatches<'static> {
7+
// Define command-line options
8+
App::new(crate_name!())
9+
.version(crate_version!())
10+
.author(crate_authors!())
11+
.about(crate_description!())
12+
// If no arguments are provided, print help
13+
.setting(AppSettings::ArgRequiredElseHelp)
14+
.arg(
15+
Arg::with_name("assembly")
16+
.short("a")
17+
.long("assembly")
18+
.takes_value(true)
19+
.value_name("title")
20+
.multiple(true)
21+
.help("Create an assembly file"),
22+
)
23+
.arg(
24+
Arg::with_name("include-in")
25+
.short("i")
26+
.long("include-in")
27+
.takes_value(true)
28+
.value_name("title")
29+
.multiple(false)
30+
.help("Create an assembly that includes the other specified modules"),
31+
)
32+
.arg(
33+
Arg::with_name("concept")
34+
.short("c")
35+
.long("concept")
36+
.takes_value(true)
37+
.value_name("title")
38+
.multiple(true)
39+
.help("Create a concept module"),
40+
)
41+
.arg(
42+
Arg::with_name("procedure")
43+
.short("p")
44+
.long("procedure")
45+
.takes_value(true)
46+
.value_name("title")
47+
.multiple(true)
48+
.help("Create a procedure module"),
49+
)
50+
.arg(
51+
Arg::with_name("reference")
52+
.short("r")
53+
.long("reference")
54+
.takes_value(true)
55+
.value_name("title")
56+
.multiple(true)
57+
.help("Create a reference module"),
58+
)
59+
// This group ensures that at least one of the assembly or module inputs is present
60+
.group(
61+
ArgGroup::with_name("modules")
62+
.args(&["assembly", "concept", "procedure", "reference"])
63+
.required(true)
64+
.multiple(true),
65+
)
66+
.arg(
67+
Arg::with_name("no-comments")
68+
.short("C")
69+
.long("no-comments")
70+
.help("Generate the file without any comments"),
71+
)
72+
.arg(
73+
Arg::with_name("no-prefixes")
74+
.short("P")
75+
.long("no-prefixes")
76+
.help("Do not use module type prefixes (e.g. `proc_`) in file names"),
77+
)
78+
.arg(
79+
Arg::with_name("target-dir")
80+
.short("-T")
81+
.long("target-dir")
82+
.takes_value(true)
83+
.value_name("directory")
84+
.help("Save the generated files in this directory"),
85+
)
86+
.get_matches()
87+
}

src/main.rs

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ use std::fs;
22
use std::io::{self, Write};
33
use std::path::PathBuf;
44

5-
extern crate clap;
6-
use clap::{
7-
crate_authors, crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgGroup,
8-
Values,
9-
};
10-
11-
extern crate colored;
125
use colored::*;
136

7+
mod cmd_line;
8+
149
// Load the AsciiDoc templates at build time
1510
const ASSEMBLY_TEMPLATE: &str = include_str!("../templates/assembly.adoc");
1611
const CONCEPT_TEMPLATE: &str = include_str!("../templates/concept.adoc");
@@ -71,86 +66,7 @@ struct Options {
7166
}
7267

7368
fn main() {
74-
// Define command-line options
75-
let cmdline_args = App::new(crate_name!())
76-
.version(crate_version!())
77-
.author(crate_authors!())
78-
.about(crate_description!())
79-
// If no arguments are provided, print help
80-
.setting(AppSettings::ArgRequiredElseHelp)
81-
.arg(
82-
Arg::with_name("assembly")
83-
.short("a")
84-
.long("assembly")
85-
.takes_value(true)
86-
.value_name("title")
87-
.multiple(true)
88-
.help("Create an assembly file"),
89-
)
90-
.arg(
91-
Arg::with_name("include-in")
92-
.short("i")
93-
.long("include-in")
94-
.takes_value(true)
95-
.value_name("title")
96-
.multiple(false)
97-
.help("Create an assembly that includes the other specified modules"),
98-
)
99-
.arg(
100-
Arg::with_name("concept")
101-
.short("c")
102-
.long("concept")
103-
.takes_value(true)
104-
.value_name("title")
105-
.multiple(true)
106-
.help("Create a concept module"),
107-
)
108-
.arg(
109-
Arg::with_name("procedure")
110-
.short("p")
111-
.long("procedure")
112-
.takes_value(true)
113-
.value_name("title")
114-
.multiple(true)
115-
.help("Create a procedure module"),
116-
)
117-
.arg(
118-
Arg::with_name("reference")
119-
.short("r")
120-
.long("reference")
121-
.takes_value(true)
122-
.value_name("title")
123-
.multiple(true)
124-
.help("Create a reference module"),
125-
)
126-
// This group ensures that at least one of the assembly or module inputs is present
127-
.group(
128-
ArgGroup::with_name("modules")
129-
.args(&["assembly", "concept", "procedure", "reference"])
130-
.required(true)
131-
.multiple(true),
132-
)
133-
.arg(
134-
Arg::with_name("no-comments")
135-
.short("C")
136-
.long("no-comments")
137-
.help("Generate the file without any comments"),
138-
)
139-
.arg(
140-
Arg::with_name("no-prefixes")
141-
.short("P")
142-
.long("no-prefixes")
143-
.help("Do not use module type prefixes (e.g. `proc_`) in file names"),
144-
)
145-
.arg(
146-
Arg::with_name("target-dir")
147-
.short("-T")
148-
.long("target-dir")
149-
.takes_value(true)
150-
.value_name("directory")
151-
.help("Save the generated files in this directory"),
152-
)
153-
.get_matches();
69+
let cmdline_args = cmd_line::get_args();
15470

15571
// Set current options based on the command-line options
15672
let options = Options {
@@ -210,7 +126,7 @@ fn main() {
210126

211127
/// Process all titles that have been specified on the command line and that belong to a single
212128
/// module type.
213-
fn process_module_type(titles: Values, module_type_str: &str, options: &Options) -> Vec<Module> {
129+
fn process_module_type(titles: clap::Values, module_type_str: &str, options: &Options) -> Vec<Module> {
214130
let mut modules_from_type = Vec::new();
215131

216132
for title in titles {

0 commit comments

Comments
 (0)