Execute command line args in order
This commit is contained in:
parent
002edeae1e
commit
c03b88eb43
3 changed files with 48 additions and 20 deletions
|
@ -68,6 +68,26 @@ impl Command {
|
|||
&Command::Close => true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn output(&self) -> String {
|
||||
match self {
|
||||
&Command::Query(ref args) => {
|
||||
format!(".{} {}", QUERY_COMMAND, args)
|
||||
},
|
||||
&Command::Transact(ref args) => {
|
||||
format!(".{} {}", TRANSACT_COMMAND, args)
|
||||
},
|
||||
&Command::Help(ref args) => {
|
||||
format!(".{} {:?}", HELP_COMMAND, args)
|
||||
},
|
||||
&Command::Open(ref args) => {
|
||||
format!(".{} {}", OPEN_COMMAND, args)
|
||||
}
|
||||
&Command::Close => {
|
||||
format!(".{}", CLOSE_COMMAND)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn command(s: &str) -> Result<Command, cli::Error> {
|
||||
|
|
|
@ -42,8 +42,8 @@ pub fn run() -> i32 {
|
|||
|
||||
opts.optopt("d", "", "The path to a database to open", "DATABASE");
|
||||
opts.optflag("h", "help", "Print this help message and exit");
|
||||
opts.optopt("q", "query", "Execute a query on startup. Queries are executed after any transacts.", "QUERY");
|
||||
opts.optopt("t", "transact", "Execute a transact on startup. Transacts are executed before queries.", "TRANSACT");
|
||||
opts.optmulti("q", "query", "Execute a query on startup. Queries are executed after any transacts.", "QUERY");
|
||||
opts.optmulti("t", "transact", "Execute a transact on startup. Transacts are executed before queries.", "TRANSACT");
|
||||
opts.optflag("v", "version", "Print version and exit");
|
||||
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
|
@ -64,22 +64,30 @@ pub fn run() -> i32 {
|
|||
return 0;
|
||||
}
|
||||
|
||||
let db_name = matches.opt_str("d");
|
||||
|
||||
let transacts = matches.opt_strs("t");
|
||||
let queries = matches.opt_strs("q");
|
||||
|
||||
let mut cmds = Vec::with_capacity(transacts.len() + queries.len());
|
||||
|
||||
for transact in transacts {
|
||||
cmds.push(command_parser::Command::Transact(transact));
|
||||
}
|
||||
for query in queries {
|
||||
cmds.push(command_parser::Command::Query(query));
|
||||
let mut last_arg: Option<&str> = None;
|
||||
let cmds:Vec<command_parser::Command> = args.iter().filter_map(|arg| {
|
||||
match last_arg {
|
||||
Some("-d") => {
|
||||
last_arg = None;
|
||||
Some(command_parser::Command::Open(arg.clone()))
|
||||
},
|
||||
Some("-q") => {
|
||||
last_arg = None;
|
||||
Some(command_parser::Command::Query(arg.clone()))
|
||||
},
|
||||
Some("-t") => {
|
||||
last_arg = None;
|
||||
Some(command_parser::Command::Transact(arg.clone()))
|
||||
},
|
||||
Some(_) |
|
||||
None => {
|
||||
last_arg = Some(&arg);
|
||||
None
|
||||
},
|
||||
}
|
||||
}).collect();
|
||||
|
||||
|
||||
let repl = repl::Repl::new(db_name);
|
||||
let repl = repl::Repl::new();
|
||||
if repl.is_ok() {
|
||||
repl.unwrap().run(Some(cmds));
|
||||
|
||||
|
|
|
@ -54,9 +54,8 @@ pub struct Repl {
|
|||
|
||||
impl Repl {
|
||||
/// Constructs a new `Repl`.
|
||||
pub fn new(db_name: Option<String>) -> Result<Repl, String> {
|
||||
let store = try!(Store::new(db_name.clone()).map_err(|e| e.to_string()));
|
||||
println!("Database {:?} opened", db_output_name(&db_name.unwrap_or("".to_string())));
|
||||
pub fn new() -> Result<Repl, String> {
|
||||
let store = Store::new(None).map_err(|e| e.to_string())?;
|
||||
Ok(Repl{
|
||||
store: store,
|
||||
})
|
||||
|
@ -68,6 +67,7 @@ impl Repl {
|
|||
|
||||
if let Some(cmds) = startup_commands {
|
||||
for command in cmds.iter() {
|
||||
println!("{}", command.output());
|
||||
self.handle_command(command.clone());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue