2021-06-19 19:07:04 +00:00
|
|
|
use arkham::{App, Command, Context, Opt};
|
2021-06-19 07:20:29 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _ = App::new()
|
|
|
|
.name("Fibonacci App")
|
|
|
|
.version("1.0")
|
2021-06-19 19:07:04 +00:00
|
|
|
.command(
|
|
|
|
Command::new("fib")
|
|
|
|
.opt(
|
|
|
|
Opt::scalar("count")
|
|
|
|
.short("n")
|
|
|
|
.long("num")
|
|
|
|
.desc("The index of the fibonacci number to return"),
|
|
|
|
)
|
|
|
|
.short_desc("Calculates a fibonacci number")
|
|
|
|
.handler(fibonacci_handler),
|
|
|
|
)
|
2021-06-19 07:20:29 +00:00
|
|
|
.run()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fibonacci_handler(_app: &App, ctx: &Context, _args: &[String]) {
|
|
|
|
let v = fibonacci(
|
2021-06-19 19:51:11 +00:00
|
|
|
ctx.get_value("count")
|
2021-06-19 07:20:29 +00:00
|
|
|
.unwrap_or("1".to_string())
|
|
|
|
.parse()
|
|
|
|
.unwrap_or(1),
|
|
|
|
);
|
|
|
|
println!("Value is: {}", v);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fibonacci(n: u32) -> u32 {
|
|
|
|
match n {
|
|
|
|
0 => 1,
|
|
|
|
1 => 1,
|
|
|
|
_ => fibonacci(n - 1) + fibonacci(n - 2),
|
|
|
|
}
|
|
|
|
}
|