Skip to content

Commit 177f740

Browse files
committed
Command is now an enum
1 parent 4eceb00 commit 177f740

File tree

4 files changed

+98
-88
lines changed

4 files changed

+98
-88
lines changed

src/librustpkg/context.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,85 +234,122 @@ impl RustcFlags {
234234
}
235235
}
236236

237+
238+
#[deriving(Eq)]
239+
pub enum Command {
240+
BuildCmd,
241+
CleanCmd,
242+
DoCmd,
243+
InfoCmd,
244+
InstallCmd,
245+
ListCmd,
246+
PreferCmd,
247+
TestCmd,
248+
InitCmd,
249+
UninstallCmd,
250+
UnpreferCmd,
251+
}
252+
253+
impl FromStr for Command {
254+
255+
fn from_str(s: &str) -> Option<Command> {
256+
match s {
257+
&"build" => Some(BuildCmd),
258+
&"clean" => Some(CleanCmd),
259+
&"do" => Some(DoCmd),
260+
&"info" => Some(InfoCmd),
261+
&"install" => Some(InstallCmd),
262+
&"list" => Some(ListCmd),
263+
&"prefer" => Some(PreferCmd),
264+
&"test" => Some(TestCmd),
265+
&"init" => Some(InitCmd),
266+
&"uninstall" => Some(UninstallCmd),
267+
&"unprefer" => Some(UnpreferCmd),
268+
_ => None
269+
}
270+
}
271+
}
272+
237273
/// Returns true if any of the flags given are incompatible with the cmd
238274
pub fn flags_forbidden_for_cmd(flags: &RustcFlags,
239275
cfgs: &[~str],
240-
cmd: &str, user_supplied_opt_level: bool) -> bool {
276+
cmd: Command, user_supplied_opt_level: bool) -> bool {
241277
let complain = |s| {
242278
println!("The {} option can only be used with the `build` command:
243279
rustpkg [options..] build {} [package-ID]", s, s);
244280
};
245281

246-
if flags.linker.is_some() && cmd != "build" && cmd != "install" {
282+
if flags.linker.is_some() && cmd != BuildCmd && cmd != InstallCmd {
247283
println("The --linker option can only be used with the build or install commands.");
248284
return true;
249285
}
250-
if flags.link_args.is_some() && cmd != "build" && cmd != "install" {
286+
if flags.link_args.is_some() && cmd != BuildCmd && cmd != InstallCmd {
251287
println("The --link-args option can only be used with the build or install commands.");
252288
return true;
253289
}
254290

255-
if !cfgs.is_empty() && cmd != "build" && cmd != "install" && cmd != "test" {
291+
if !cfgs.is_empty() && cmd != BuildCmd && cmd != InstallCmd && cmd != TestCmd {
256292
println("The --cfg option can only be used with the build, test, or install commands.");
257293
return true;
258294
}
259295

260-
if user_supplied_opt_level && cmd != "build" && cmd != "install" {
296+
if user_supplied_opt_level && cmd != BuildCmd && cmd != InstallCmd {
261297
println("The -O and --opt-level options can only be used with the build \
262298
or install commands.");
263299
return true;
264300
}
265301

266-
if flags.save_temps && cmd != "build" && cmd != "install" {
302+
if flags.save_temps && cmd != BuildCmd && cmd != InstallCmd {
267303
println("The --save-temps option can only be used with the build \
268304
or install commands.");
269305
return true;
270306
}
271307

272-
if flags.target.is_some() && cmd != "build" && cmd != "install" {
308+
if flags.target.is_some() && cmd != BuildCmd && cmd != InstallCmd {
273309
println("The --target option can only be used with the build \
274310
or install commands.");
275311
return true;
276312
}
277-
if flags.target_cpu.is_some() && cmd != "build" && cmd != "install" {
313+
if flags.target_cpu.is_some() && cmd != BuildCmd && cmd != InstallCmd {
278314
println("The --target-cpu option can only be used with the build \
279315
or install commands.");
280316
return true;
281317
}
282-
if flags.experimental_features.is_some() && cmd != "build" && cmd != "install" {
318+
if flags.experimental_features.is_some() && cmd != BuildCmd && cmd != InstallCmd {
283319
println("The -Z option can only be used with the build or install commands.");
284320
return true;
285321
}
286322

287323
match flags.compile_upto {
288-
Link if cmd != "build" => {
324+
Link if cmd != BuildCmd => {
289325
complain("--no-link");
290326
true
291327
}
292-
Trans if cmd != "build" => {
328+
Trans if cmd != BuildCmd => {
293329
complain("--no-trans");
294330
true
295331
}
296-
Assemble if cmd != "build" => {
332+
Assemble if cmd != BuildCmd => {
297333
complain("-S");
298334
true
299335
}
300-
Pretty if cmd != "build" => {
336+
Pretty if cmd != BuildCmd => {
301337
complain("--pretty");
302338
true
303339
}
304-
Analysis if cmd != "build" => {
340+
Analysis if cmd != BuildCmd => {
305341
complain("--parse-only");
306342
true
307343
}
308-
LLVMCompileBitcode if cmd != "build" => {
344+
LLVMCompileBitcode if cmd != BuildCmd => {
309345
complain("--emit-llvm");
310346
true
311347
}
312-
LLVMAssemble if cmd != "build" => {
348+
LLVMAssemble if cmd != BuildCmd => {
313349
complain("--emit-llvm");
314350
true
315351
}
316352
_ => false
317353
}
318354
}
355+

src/librustpkg/lib.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ use workspace::determine_destination;
4545
use context::{Context, BuildContext,
4646
RustcFlags, Trans, Link, Nothing, Pretty, Analysis, Assemble,
4747
LLVMAssemble, LLVMCompileBitcode};
48+
use context::{Command, BuildCmd, CleanCmd, DoCmd, InfoCmd, InstallCmd, ListCmd,
49+
PreferCmd, TestCmd, InitCmd, UninstallCmd, UnpreferCmd};
4850
use crate_id::CrateId;
4951
use package_source::PkgSrc;
5052
use target::{WhatToBuild, Everything, is_lib, is_main, is_test, is_bench};
@@ -205,7 +207,7 @@ impl<'a> PkgScript<'a> {
205207
}
206208

207209
pub trait CtxMethods {
208-
fn run(&self, cmd: &str, args: ~[~str]);
210+
fn run(&self, cmd: Command, args: ~[~str]);
209211
fn do_cmd(&self, _cmd: &str, _pkgname: &str);
210212
/// Returns a pair of the selected package ID, and the destination workspace
211213
fn build_args(&self, args: ~[~str], what: &WhatToBuild) -> Option<(CrateId, Path)>;
@@ -281,13 +283,13 @@ impl CtxMethods for BuildContext {
281283
Some((crateid, dest_ws))
282284
}
283285
}
284-
fn run(&self, cmd: &str, args: ~[~str]) {
286+
fn run(&self, cmd: Command, args: ~[~str]) {
285287
let cwd = os::getcwd();
286288
match cmd {
287-
"build" => {
289+
BuildCmd => {
288290
self.build_args(args, &WhatToBuild::new(MaybeCustom, Everything));
289291
}
290-
"clean" => {
292+
CleanCmd => {
291293
if args.len() < 1 {
292294
match cwd_to_workspace() {
293295
None => { usage::clean(); return }
@@ -304,17 +306,17 @@ impl CtxMethods for BuildContext {
304306
self.clean(&cwd, &crateid); // tjc: should use workspace, not cwd
305307
}
306308
}
307-
"do" => {
309+
DoCmd => {
308310
if args.len() < 2 {
309311
return usage::do_cmd();
310312
}
311313

312314
self.do_cmd(args[0].clone(), args[1].clone());
313315
}
314-
"info" => {
316+
InfoCmd => {
315317
self.info();
316318
}
317-
"install" => {
319+
InstallCmd => {
318320
if args.len() < 1 {
319321
match cwd_to_workspace() {
320322
None if dir_has_crate_file(&cwd) => {
@@ -360,21 +362,21 @@ impl CtxMethods for BuildContext {
360362
}
361363
}
362364
}
363-
"list" => {
365+
ListCmd => {
364366
println("Installed packages:");
365367
installed_packages::list_installed_packages(|pkg_id| {
366368
pkg_id.path.display().with_str(|s| println(s));
367369
true
368370
});
369371
}
370-
"prefer" => {
372+
PreferCmd => {
371373
if args.len() < 1 {
372374
return usage::uninstall();
373375
}
374376

375377
self.prefer(args[0], None);
376378
}
377-
"test" => {
379+
TestCmd => {
378380
// Build the test executable
379381
let maybe_id_and_workspace = self.build_args(args,
380382
&WhatToBuild::new(MaybeCustom, Tests));
@@ -388,14 +390,14 @@ impl CtxMethods for BuildContext {
388390
}
389391
}
390392
}
391-
"init" => {
393+
InitCmd => {
392394
if args.len() != 0 {
393395
return usage::init();
394396
} else {
395397
self.init();
396398
}
397399
}
398-
"uninstall" => {
400+
UninstallCmd => {
399401
if args.len() < 1 {
400402
return usage::uninstall();
401403
}
@@ -417,14 +419,13 @@ impl CtxMethods for BuildContext {
417419
});
418420
}
419421
}
420-
"unprefer" => {
422+
UnpreferCmd => {
421423
if args.len() < 1 {
422424
return usage::unprefer();
423425
}
424426

425427
self.unprefer(args[0], None);
426428
}
427-
_ => fail!("I don't know the command `{}`", cmd)
428429
}
429430
}
430431

@@ -864,38 +865,19 @@ pub fn main_args(args: &[~str]) -> int {
864865
experimental_features: experimental_features
865866
};
866867

867-
let mut cmd_opt = None;
868-
for a in args.iter() {
869-
if util::is_cmd(*a) {
870-
cmd_opt = Some(a);
871-
break;
872-
}
873-
}
874-
let cmd = match cmd_opt {
868+
let cmd_opt = args.iter().filter_map( |s| from_str(s.clone())).next();
869+
let command = match(cmd_opt) {
875870
None => {
876871
usage::general();
877872
return 0;
878873
}
879874
Some(cmd) => {
880875
let bad_option = context::flags_forbidden_for_cmd(&rustc_flags,
881876
cfgs,
882-
*cmd,
877+
cmd,
883878
user_supplied_opt_level);
884879
if help || bad_option {
885-
match *cmd {
886-
~"build" => usage::build(),
887-
~"clean" => usage::clean(),
888-
~"do" => usage::do_cmd(),
889-
~"info" => usage::info(),
890-
~"install" => usage::install(),
891-
~"list" => usage::list(),
892-
~"prefer" => usage::prefer(),
893-
~"test" => usage::test(),
894-
~"init" => usage::init(),
895-
~"uninstall" => usage::uninstall(),
896-
~"unprefer" => usage::unprefer(),
897-
_ => usage::general()
898-
};
880+
usage::usage_for_command(cmd);
899881
if bad_option {
900882
return BAD_FLAG_CODE;
901883
}
@@ -909,9 +891,10 @@ pub fn main_args(args: &[~str]) -> int {
909891
};
910892

911893
// Pop off all flags, plus the command
912-
let remaining_args = args.iter().skip_while(|s| !util::is_cmd(**s));
913-
// I had to add this type annotation to get the code to typecheck
914-
let mut remaining_args: ~[~str] = remaining_args.map(|s| (*s).clone()).collect();
894+
let mut remaining_args: ~[~str] = args.iter().skip_while(|&s| {
895+
let maybe_command: Option<Command> = from_str(*s);
896+
maybe_command.is_none()
897+
}).map(|s| s.clone()).collect();
915898
remaining_args.shift();
916899
let sroot = match supplied_sysroot {
917900
Some(s) => Path::new(s),
@@ -923,7 +906,6 @@ pub fn main_args(args: &[~str]) -> int {
923906
debug!("Will store workcache in {}", ws.display());
924907

925908
let rm_args = remaining_args.clone();
926-
let sub_cmd = cmd.clone();
927909
// Wrap the rest in task::try in case of a condition failure in a task
928910
let result = do task::try {
929911
BuildContext {
@@ -935,7 +917,7 @@ pub fn main_args(args: &[~str]) -> int {
935917
},
936918
workcache_context: api::default_context(sroot.clone(),
937919
default_workspace()).workcache_context
938-
}.run(sub_cmd, rm_args.clone())
920+
}.run(command, rm_args.clone())
939921
};
940922
// FIXME #9262: This is using the same error code for all errors,
941923
// and at least one test case succeeds if rustpkg returns COPY_FAILED_CODE,

src/librustpkg/usage.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use context::Command;
12+
1113
pub fn general() {
1214
println("Usage: rustpkg [options] <cmd> [args..]
1315
@@ -154,3 +156,22 @@ This will turn the current working directory into a workspace. The first
154156
command you run when starting off a new project.
155157
");
156158
}
159+
160+
pub fn usage_for_command(command: Command){
161+
match command {
162+
BuildCmd => build(),
163+
CleanCmd => clean(),
164+
DoCmd => do_cmd(),
165+
InfoCmd => info(),
166+
InstallCmd => install(),
167+
ListCmd => list(),
168+
PreferCmd => prefer(),
169+
TestCmd => test(),
170+
InitCmd => init(),
171+
UninstallCmd => uninstall(),
172+
UnpreferCmd => unprefer(),
173+
};
174+
}
175+
176+
177+

0 commit comments

Comments
 (0)