1
- use crate :: { config:: Config , Command } ;
1
+ use crate :: { config:: Config , Command , ErrorString } ;
2
2
use std:: path:: { Path , PathBuf } ;
3
3
use std:: { env, mem} ;
4
4
5
- pub ( crate ) fn parse_args ( ) -> Command {
6
- let mut args = env:: args ( ) . skip ( 1 ) ;
5
+ pub ( crate ) fn parse_args ( ) -> Result < Command , ErrorString > {
6
+ let mut args = env:: args ( ) ;
7
+ let executable_name = args. next ( ) . ok_or ( "no first argument (executable name)" ) ?;
7
8
let first = args. next ( ) ;
8
9
match first. as_ref ( ) . map ( |s| s. as_str ( ) ) {
9
10
Some ( "build" ) => parse_build_args ( args) ,
10
- Some ( "run" ) => match parse_build_args ( args) {
11
+ Some ( "bootimage" ) if executable_name. ends_with ( "cargo-bootimage" ) => parse_build_args ( args)
12
+ . map ( |cmd| match cmd {
13
+ Command :: BuildHelp => Command :: CargoBootimageHelp ,
14
+ cmd => cmd,
15
+ } ) ,
16
+ Some ( "run" ) => parse_build_args ( args) . map ( |cmd| match cmd {
11
17
Command :: Build ( args) => Command :: Run ( args) ,
12
18
Command :: BuildHelp => Command :: RunHelp ,
13
19
cmd => cmd,
14
- } ,
15
- Some ( "test" ) => match parse_build_args ( args) {
20
+ } ) ,
21
+ Some ( "test" ) => parse_build_args ( args) . map ( |cmd| match cmd {
16
22
Command :: Build ( args) => {
17
23
assert_eq ! (
18
24
args. bin_name, None ,
@@ -22,15 +28,15 @@ pub(crate) fn parse_args() -> Command {
22
28
}
23
29
Command :: BuildHelp => Command :: TestHelp ,
24
30
cmd => cmd,
25
- } ,
31
+ } ) ,
26
32
Some ( "runner" ) => parse_runner_args ( args) ,
27
- Some ( "--help" ) | Some ( "-h" ) => Command :: Help ,
28
- Some ( "--version" ) => Command :: Version ,
29
- _ => Command :: NoSubcommand ,
33
+ Some ( "--help" ) | Some ( "-h" ) => Ok ( Command :: Help ) ,
34
+ Some ( "--version" ) => Ok ( Command :: Version ) ,
35
+ _ => Ok ( Command :: NoSubcommand ) ,
30
36
}
31
37
}
32
38
33
- fn parse_build_args < A > ( args : A ) -> Command
39
+ fn parse_build_args < A > ( args : A ) -> Result < Command , ErrorString >
34
40
where
35
41
A : Iterator < Item = String > ,
36
42
{
@@ -42,12 +48,12 @@ where
42
48
let mut run_args = Vec :: new ( ) ;
43
49
let mut run_args_started = false ;
44
50
{
45
- fn set < T > ( arg : & mut Option < T > , value : Option < T > ) {
51
+ fn set < T > ( arg : & mut Option < T > , value : Option < T > ) -> Result < ( ) , ErrorString > {
46
52
let previous = mem:: replace ( arg, value) ;
47
- assert ! (
48
- previous . is_none ( ) ,
49
- "multiple arguments of same type provided"
50
- )
53
+ if previous . is_some ( ) {
54
+ Err ( "multiple arguments of same type provided" ) ?
55
+ }
56
+ Ok ( ( ) )
51
57
} ;
52
58
53
59
let mut arg_iter = args. into_iter ( ) ;
@@ -58,14 +64,14 @@ where
58
64
}
59
65
match arg. as_ref ( ) {
60
66
"--help" | "-h" => {
61
- return Command :: BuildHelp ;
67
+ return Ok ( Command :: BuildHelp ) ;
62
68
}
63
69
"--version" => {
64
- return Command :: Version ;
70
+ return Ok ( Command :: Version ) ;
65
71
}
66
72
"--bin" => {
67
73
let next = arg_iter. next ( ) ;
68
- set ( & mut bin_name, next. clone ( ) ) ;
74
+ set ( & mut bin_name, next. clone ( ) ) ? ;
69
75
cargo_args. push ( arg) ;
70
76
if let Some ( next) = next {
71
77
cargo_args. push ( next) ;
@@ -75,12 +81,12 @@ where
75
81
set (
76
82
& mut bin_name,
77
83
Some ( String :: from ( arg. trim_start_matches ( "--bin=" ) ) ) ,
78
- ) ;
84
+ ) ? ;
79
85
cargo_args. push ( arg) ;
80
86
}
81
87
"--target" => {
82
88
let next = arg_iter. next ( ) ;
83
- set ( & mut target, next. clone ( ) ) ;
89
+ set ( & mut target, next. clone ( ) ) ? ;
84
90
cargo_args. push ( arg) ;
85
91
if let Some ( next) = next {
86
92
cargo_args. push ( next) ;
90
96
set (
91
97
& mut target,
92
98
Some ( String :: from ( arg. trim_start_matches ( "--target=" ) ) ) ,
93
- ) ;
99
+ ) ? ;
94
100
cargo_args. push ( arg) ;
95
101
}
96
102
"--manifest-path" => {
@@ -102,7 +108,7 @@ where
102
108
. canonicalize ( )
103
109
. expect ( "--manifest-path invalid" )
104
110
} ) ,
105
- ) ;
111
+ ) ? ;
106
112
cargo_args. push ( arg) ;
107
113
if let Some ( next) = next {
108
114
cargo_args. push ( next) ;
@@ -112,11 +118,11 @@ where
112
118
let path = Path :: new ( arg. trim_start_matches ( "--manifest-path=" ) )
113
119
. canonicalize ( )
114
120
. expect ( "--manifest-path invalid" ) ;
115
- set ( & mut manifest_path, Some ( path) ) ;
121
+ set ( & mut manifest_path, Some ( path) ) ? ;
116
122
cargo_args. push ( arg) ;
117
123
}
118
124
"--release" => {
119
- set ( & mut release, Some ( true ) ) ;
125
+ set ( & mut release, Some ( true ) ) ? ;
120
126
cargo_args. push ( arg) ;
121
127
}
122
128
"--" => {
@@ -129,14 +135,14 @@ where
129
135
}
130
136
}
131
137
132
- Command :: Build ( Args {
138
+ Ok ( Command :: Build ( Args {
133
139
cargo_args,
134
140
run_args,
135
141
bin_name,
136
142
target,
137
143
manifest_path,
138
144
release : release. unwrap_or ( false ) ,
139
- } )
145
+ } ) )
140
146
}
141
147
142
148
#[ derive( Debug , Clone ) ]
@@ -196,42 +202,44 @@ impl Args {
196
202
}
197
203
}
198
204
199
- fn parse_runner_args < A > ( args : A ) -> Command
205
+ fn parse_runner_args < A > ( args : A ) -> Result < Command , ErrorString >
200
206
where
201
207
A : Iterator < Item = String > ,
202
208
{
203
209
let mut arg_iter = args. into_iter ( ) . fuse ( ) ;
204
210
let executable = PathBuf :: from (
205
211
arg_iter
206
212
. next ( )
207
- . expect ( "excepted path to kernel executable as first argument" ) ,
213
+ . ok_or ( "excepted path to kernel executable as first argument" ) ? ,
208
214
)
209
215
. canonicalize ( )
210
- . expect ( "Failed to canonicalize executable path" ) ;
216
+ . map_err ( |err| format ! ( "Failed to canonicalize executable path: {}" , err ) ) ? ;
211
217
let mut run_command = None ;
212
218
213
219
loop {
214
220
match arg_iter. next ( ) . as_ref ( ) . map ( |s| s. as_str ( ) ) {
215
221
Some ( "--command" ) => {
216
222
let old = mem:: replace ( & mut run_command, Some ( arg_iter. collect ( ) ) ) ;
217
- assert ! ( old. is_none( ) , "multiple `--command` arguments" ) ;
223
+ if !old. is_none ( ) {
224
+ Err ( "multiple `--command` arguments" ) ?;
225
+ }
218
226
break ;
219
227
}
220
228
Some ( "--help" ) | Some ( "-h" ) => {
221
- return Command :: RunnerHelp ;
229
+ return Ok ( Command :: RunnerHelp ) ;
222
230
}
223
231
Some ( "--version" ) => {
224
- return Command :: Version ;
232
+ return Ok ( Command :: Version ) ;
225
233
}
226
234
None => break ,
227
- Some ( arg) => panic ! ( "unexpected argument `{}`" , arg) ,
235
+ Some ( arg) => Err ( format ! ( "unexpected argument `{}`" , arg) ) ? ,
228
236
}
229
237
}
230
238
231
- Command :: Runner ( RunnerArgs {
239
+ Ok ( Command :: Runner ( RunnerArgs {
232
240
executable,
233
241
run_command,
234
- } )
242
+ } ) )
235
243
}
236
244
237
245
#[ derive( Debug , Clone ) ]
0 commit comments