bdrck_params/
argument.rs

1// Copyright 2015 Axel Rasmussen
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt;
16use std::option::Option as Optional;
17use std::string::String;
18
19/// An argument is a positional parameter. It must come after any Options
20/// the command supports, and can have a default value if it is not
21/// specified by the user explicitly.
22///
23/// The final Argument for a Command can be variadic (that is, it can
24/// accept more than one value), but whether or not this is the case is a
25/// property of the Command, not of the Argument (because the Argument only
26/// stores a description of the argument, not its final value).
27#[derive(Debug)]
28pub struct Argument {
29    pub name: String,
30    pub help: String,
31    pub default_value: Optional<Vec<String>>,
32}
33
34impl Argument {
35    pub fn new(name: &str, help: &str, default_value: Optional<Vec<String>>) -> Argument {
36        Argument {
37            name: name.to_owned(),
38            help: help.to_owned(),
39            default_value: default_value,
40        }
41    }
42}
43
44impl fmt::Display for Argument {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        write!(f, "{} - {}", self.name, self.help)?;
47        if let Some(default) = self.default_value.as_ref() {
48            write!(f, " [Default: {}]", &default[..].join(", "))?;
49        }
50        Ok(())
51    }
52}