options.rs 21.4 KB
Newer Older
B
Ben S 已提交
1
use dir::Dir;
B
Ben S 已提交
2
use file::File;
3
use column::Column;
B
Ben S 已提交
4
use column::Column::*;
5
use output::{Grid, Details};
B
Ben S 已提交
6
use term::dimensions;
N
nwin 已提交
7
use xattr;
B
Ben S 已提交
8

B
Ben S 已提交
9
use std::cmp::Ordering;
10
use std::fmt;
11
use std::num::ParseIntError;
B
Ben S 已提交
12

13 14 15
use getopts;
use natord;

B
Ben S 已提交
16 17
use datetime::local::{LocalDateTime, DatePiece};

B
Ben S 已提交
18
use self::Misfire::*;
19

B
Ben S 已提交
20 21
/// The *Options* struct represents a parsed version of the user's
/// command-line options.
22
#[derive(PartialEq, Debug, Copy)]
B
Ben S 已提交
23
pub struct Options {
24
    pub dir_action: DirAction,
25
    pub filter: FileFilter,
26
    pub view: View,
27 28 29 30
}

#[derive(PartialEq, Debug, Copy)]
pub struct FileFilter {
B
Ben S 已提交
31
    list_dirs_first: bool,
B
Benjamin Sago 已提交
32 33 34
    reverse: bool,
    show_invisibles: bool,
    sort_field: SortField,
B
Ben S 已提交
35 36
}

37
#[derive(PartialEq, Debug, Copy)]
38 39 40 41 42 43
pub enum View {
    Details(Details),
    Lines,
    Grid(Grid),
}

B
Ben S 已提交
44
impl Options {
B
Ben S 已提交
45 46

    /// Call getopts on the given slice of command-line strings.
47
    pub fn getopts(args: &[String]) -> Result<(Options, Vec<String>), Misfire> {
B
Ben S 已提交
48 49 50 51 52 53 54
        let mut opts = getopts::Options::new();
        opts.optflag("1", "oneline",   "display one entry per line");
        opts.optflag("a", "all",       "show dot-files");
        opts.optflag("b", "binary",    "use binary prefixes in file sizes");
        opts.optflag("B", "bytes",     "list file sizes in bytes, without prefixes");
        opts.optflag("d", "list-dirs", "list directories as regular files");
        opts.optflag("g", "group",     "show group as well as user");
B
Ben S 已提交
55
        opts.optflag("",  "group-directories-first", "list directories before other files");
B
Ben S 已提交
56 57 58
        opts.optflag("h", "header",    "show a header row at the top");
        opts.optflag("H", "links",     "show number of hard links");
        opts.optflag("i", "inode",     "show each file's inode number");
B
Ben S 已提交
59
        opts.optflag("l", "long",      "display extended details and attributes");
60
        opts.optopt ("L", "level",     "maximum depth of recursion", "DEPTH");
61
        opts.optflag("m", "modified",  "display timestamp of most recent modification");
B
Ben S 已提交
62 63 64 65
        opts.optflag("r", "reverse",   "reverse order of files");
        opts.optflag("R", "recurse",   "recurse into directories");
        opts.optopt ("s", "sort",      "field to sort by", "WORD");
        opts.optflag("S", "blocks",    "show number of file system blocks");
B
Ben S 已提交
66
        opts.optopt ("t", "time",      "which timestamp to show for a file", "WORD");
B
Ben S 已提交
67
        opts.optflag("T", "tree",      "recurse into subdirectories in a tree view");
68 69
        opts.optflag("u", "accessed",  "display timestamp of last access for a file");
        opts.optflag("U", "created",   "display timestamp of creation for a file");
B
Ben S 已提交
70
        opts.optflag("x", "across",    "sort multi-column view entries across");
B
Ben S 已提交
71 72

        opts.optflag("",  "version",   "display version of exa");
B
Ben S 已提交
73
        opts.optflag("?", "help",      "show list of command-line options");
B
Ben S 已提交
74

75 76 77 78
        if xattr::feature_implemented() {
            opts.optflag("@", "extended", "display extended attribute keys and sizes in long (-l) output");
        }

B
Ben S 已提交
79
        let matches = match opts.parse(args) {
80
            Ok(m) => m,
B
Ben S 已提交
81
            Err(e) => return Err(Misfire::InvalidOptions(e)),
B
Ben S 已提交
82
        };
B
Ben S 已提交
83

B
Ben S 已提交
84
        if matches.opt_present("help") {
B
Ben S 已提交
85
            return Err(Misfire::Help(opts.usage("Usage:\n  exa [options] [files...]")));
B
Ben S 已提交
86
        }
B
Ben S 已提交
87 88 89
        else if matches.opt_present("version") {
            return Err(Misfire::Version);
        }
B
Ben S 已提交
90

91 92 93 94 95
        let sort_field = match matches.opt_str("sort") {
            Some(word) => try!(SortField::from_word(word)),
            None => SortField::Name,
        };

96
        let filter = FileFilter {
B
Ben S 已提交
97
            list_dirs_first: matches.opt_present("group-directories-first"),
98 99 100 101 102 103 104 105 106 107 108 109
            reverse:         matches.opt_present("reverse"),
            show_invisibles: matches.opt_present("all"),
            sort_field:      sort_field,
        };

        let path_strs = if matches.free.is_empty() {
            vec![ ".".to_string() ]
        }
        else {
            matches.free.clone()
        };

110 111 112
        let dir_action = try!(DirAction::deduce(&matches));
        let view = try!(View::deduce(&matches, filter, dir_action));

113
        Ok((Options {
114 115
            dir_action: dir_action,
            view:       view,
116 117
            filter:     filter,
        }, path_strs))
B
Ben S 已提交
118
    }
B
Ben S 已提交
119

B
Ben S 已提交
120
    pub fn transform_files<'a>(&self, files: &mut Vec<File<'a>>) {
121 122 123
        self.filter.transform_files(files)
    }
}
B
Ben S 已提交
124

125
impl FileFilter {
B
Ben S 已提交
126
    /// Transform the files (sorting, reversing, filtering) before listing them.
B
Ben S 已提交
127
    pub fn transform_files<'a>(&self, files: &mut Vec<File<'a>>) {
B
Ben S 已提交
128 129

        if !self.show_invisibles {
B
Ben S 已提交
130
            files.retain(|f| !f.is_dotfile());
B
Ben S 已提交
131
        }
132 133 134

        match self.sort_field {
            SortField::Unsorted => {},
B
Ben S 已提交
135
            SortField::Name => files.sort_by(|a, b| natord::compare(&*a.name, &*b.name)),
136 137
            SortField::Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
            SortField::FileInode => files.sort_by(|a, b| a.stat.unstable.inode.cmp(&b.stat.unstable.inode)),
C
Corey Ford 已提交
138 139 140
            SortField::Extension => files.sort_by(|a, b| match a.ext.cmp(&b.ext) {
                Ordering::Equal => natord::compare(&*a.name, &*b.name),
                order => order
141
            }),
B
Ben S 已提交
142 143 144
            SortField::ModifiedDate => files.sort_by(|a, b| a.stat.modified.cmp(&b.stat.modified)),
            SortField::AccessedDate => files.sort_by(|a, b| a.stat.accessed.cmp(&b.stat.accessed)),
            SortField::CreatedDate  => files.sort_by(|a, b| a.stat.created.cmp(&b.stat.created)),
B
Ben S 已提交
145
        }
146 147 148 149

        if self.reverse {
            files.reverse();
        }
B
Ben S 已提交
150 151 152 153 154

        if self.list_dirs_first {
            // This relies on the fact that sort_by is stable.
            files.sort_by(|a, b| b.is_directory().cmp(&a.is_directory()));
        }
155 156
    }
}
B
Ben S 已提交
157

158
/// User-supplied field to sort by.
B
Ben S 已提交
159
#[derive(PartialEq, Debug, Copy)]
B
Ben S 已提交
160
pub enum SortField {
B
Ben S 已提交
161 162
    Unsorted, Name, Extension, Size, FileInode,
    ModifiedDate, AccessedDate, CreatedDate,
B
Ben S 已提交
163 164 165 166 167 168
}

impl SortField {

    /// Find which field to use based on a user-supplied word.
    fn from_word(word: String) -> Result<SortField, Misfire> {
169
        match &word[..] {
B
Ben S 已提交
170 171 172 173 174 175 176 177 178
            "name" | "filename"  => Ok(SortField::Name),
            "size" | "filesize"  => Ok(SortField::Size),
            "ext"  | "extension" => Ok(SortField::Extension),
            "mod"  | "modified"  => Ok(SortField::ModifiedDate),
            "acc"  | "accessed"  => Ok(SortField::AccessedDate),
            "cr"   | "created"   => Ok(SortField::CreatedDate),
            "none"               => Ok(SortField::Unsorted),
            "inode"              => Ok(SortField::FileInode),
            field                => Err(SortField::none(field))
B
Ben S 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        }
    }

    /// How to display an error when the word didn't match with anything.
    fn none(field: &str) -> Misfire {
        Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--sort {}", field)))
    }
}

/// One of these things could happen instead of listing files.
#[derive(PartialEq, Debug)]
pub enum Misfire {

    /// The getopts crate didn't like these arguments.
    InvalidOptions(getopts::Fail),

    /// The user asked for help. This isn't strictly an error, which is why
    /// this enum isn't named Error!
    Help(String),

B
Ben S 已提交
199 200 201
    /// The user wanted the version number.
    Version,

202
    /// Two options were given that conflict with one another.
B
Ben S 已提交
203 204 205 206 207
    Conflict(&'static str, &'static str),

    /// An option was given that does nothing when another one either is or
    /// isn't present.
    Useless(&'static str, bool, &'static str),
208

B
Ben S 已提交
209 210 211 212
    /// An option was given that does nothing when either of two other options
    /// are not present.
    Useless2(&'static str, &'static str, &'static str),

213 214
    /// A numeric option was given that failed to be parsed as a number.
    FailedParse(ParseIntError),
B
Ben S 已提交
215 216 217 218
}

impl Misfire {
    /// The OS return code this misfire should signify.
B
Ben S 已提交
219
    pub fn error_code(&self) -> i32 {
B
Ben S 已提交
220 221 222 223 224 225 226 227 228 229
        if let Help(_) = *self { 2 }
                          else { 3 }
    }
}

impl fmt::Display for Misfire {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            InvalidOptions(ref e) => write!(f, "{}", e),
            Help(ref text)        => write!(f, "{}", text),
B
Ben S 已提交
230
            Version               => write!(f, "exa {}", env!("CARGO_PKG_VERSION")),
B
Benjamin Sago 已提交
231 232 233
            Conflict(a, b)        => write!(f, "Option --{} conflicts with option {}.", a, b),
            Useless(a, false, b)  => write!(f, "Option --{} is useless without option --{}.", a, b),
            Useless(a, true, b)   => write!(f, "Option --{} is useless given option --{}.", a, b),
B
Ben S 已提交
234
            Useless2(a, b1, b2)   => write!(f, "Option --{} is useless without options --{} or --{}.", a, b1, b2),
235
            FailedParse(ref e)    => write!(f, "Failed to parse number: {}", e),
B
Ben S 已提交
236 237 238 239
        }
    }
}

240
impl View {
241
    pub fn deduce(matches: &getopts::Matches, filter: FileFilter, dir_action: DirAction) -> Result<View, Misfire> {
242 243 244 245 246 247 248 249 250 251
        if matches.opt_present("long") {
            if matches.opt_present("across") {
                Err(Misfire::Useless("across", true, "long"))
            }
            else if matches.opt_present("oneline") {
                Err(Misfire::Useless("oneline", true, "long"))
            }
            else {
                let details = Details {
                        columns: try!(Columns::deduce(matches)),
252
                        header: matches.opt_present("header"),
253
                        recurse: dir_action.recurse_options().map(|o| (o, filter)),
N
nwin 已提交
254
                        xattr: xattr::feature_implemented() && matches.opt_present("extended"),
255 256 257 258
                };

                Ok(View::Details(details))
            }
B
Ben S 已提交
259
        }
260 261
        else if matches.opt_present("binary") {
            Err(Misfire::Useless("binary", false, "long"))
B
Ben S 已提交
262
        }
263 264
        else if matches.opt_present("bytes") {
            Err(Misfire::Useless("bytes", false, "long"))
265
        }
266 267
        else if matches.opt_present("inode") {
            Err(Misfire::Useless("inode", false, "long"))
268
        }
269 270
        else if matches.opt_present("links") {
            Err(Misfire::Useless("links", false, "long"))
271
        }
272 273 274 275 276 277
        else if matches.opt_present("header") {
            Err(Misfire::Useless("header", false, "long"))
        }
        else if matches.opt_present("blocks") {
            Err(Misfire::Useless("blocks", false, "long"))
        }
B
Ben S 已提交
278 279 280
        else if matches.opt_present("time") {
            Err(Misfire::Useless("time", false, "long"))
        }
B
Ben S 已提交
281 282 283
        else if matches.opt_present("tree") {
            Err(Misfire::Useless("tree", false, "long"))
        }
B
Ben S 已提交
284 285 286
        else if matches.opt_present("level") && !matches.opt_present("recurse") {
            Err(Misfire::Useless2("level", "recurse", "tree"))
        }
N
nwin 已提交
287
        else if xattr::feature_implemented() && matches.opt_present("extended") {
N
nwin 已提交
288 289
            Err(Misfire::Useless("extended", false, "long"))
        }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        else if matches.opt_present("oneline") {
            if matches.opt_present("across") {
                Err(Misfire::Useless("across", true, "oneline"))
            }
            else {
                Ok(View::Lines)
            }
        }
        else {
            if let Some((width, _)) = dimensions() {
                let grid = Grid {
                    across: matches.opt_present("across"),
                    console_width: width
                };

                Ok(View::Grid(grid))
            }
            else {
                // If the terminal width couldn't be matched for some reason, such
                // as the program's stdout being connected to a file, then
                // fallback to the lines view.
                Ok(View::Lines)
            }
B
Ben S 已提交
313
        }
314 315
    }
}
B
Ben S 已提交
316

B
Ben S 已提交
317
#[derive(PartialEq, Debug, Copy, Clone)]
318 319 320 321 322
pub enum SizeFormat {
    DecimalBytes,
    BinaryBytes,
    JustBytes,
}
B
Ben S 已提交
323

324 325 326 327 328 329 330 331 332 333 334
impl SizeFormat {
    pub fn deduce(matches: &getopts::Matches) -> Result<SizeFormat, Misfire> {
        let binary = matches.opt_present("binary");
        let bytes  = matches.opt_present("bytes");

        match (binary, bytes) {
            (true,  true ) => Err(Misfire::Conflict("binary", "bytes")),
            (true,  false) => Ok(SizeFormat::BinaryBytes),
            (false, true ) => Ok(SizeFormat::JustBytes),
            (false, false) => Ok(SizeFormat::DecimalBytes),
        }
335 336 337
    }
}

B
Ben S 已提交
338
#[derive(PartialEq, Debug, Copy, Clone)]
339 340 341 342 343 344 345
pub enum TimeType {
    FileAccessed,
    FileModified,
    FileCreated,
}

impl TimeType {
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    pub fn header(&self) -> &'static str {
        match *self {
            TimeType::FileAccessed => "Date Accessed",
            TimeType::FileModified => "Date Modified",
            TimeType::FileCreated  => "Date Created",
        }
    }
}

#[derive(PartialEq, Debug, Copy)]
pub struct TimeTypes {
    accessed: bool,
    modified: bool,
    created:  bool,
}

impl TimeTypes {
363 364

    /// Find which field to use based on a user-supplied word.
365
    fn deduce(matches: &getopts::Matches) -> Result<TimeTypes, Misfire> {
366
        let possible_word = matches.opt_str("time");
367 368 369
        let modified = matches.opt_present("modified");
        let created  = matches.opt_present("created");
        let accessed = matches.opt_present("accessed");
370 371

        if let Some(word) = possible_word {
372 373 374 375 376 377 378 379 380 381
            if modified {
                return Err(Misfire::Useless("modified", true, "time"));
            }
            else if created {
                return Err(Misfire::Useless("created", true, "time"));
            }
            else if accessed {
                return Err(Misfire::Useless("accessed", true, "time"));
            }

382
            match &word[..] {
383 384 385 386
                "mod" | "modified"  => Ok(TimeTypes { accessed: false, modified: true, created: false }),
                "acc" | "accessed"  => Ok(TimeTypes { accessed: true, modified: false, created: false }),
                "cr"  | "created"   => Ok(TimeTypes { accessed: false, modified: false, created: true }),
                field   => Err(TimeTypes::none(field)),
387 388 389
            }
        }
        else {
390 391 392 393 394 395
            if modified || created || accessed {
                Ok(TimeTypes { accessed: accessed, modified: modified, created: created })
            }
            else {
                Ok(TimeTypes { accessed: false, modified: true, created: false })
            }
396 397 398 399 400 401 402 403
        }
    }

    /// How to display an error when the word didn't match with anything.
    fn none(field: &str) -> Misfire {
        Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--time {}", field)))
    }
}
404

405 406 407
/// What to do when encountering a directory?
#[derive(PartialEq, Debug, Copy)]
pub enum DirAction {
408 409 410
    AsFile,
    List,
    Recurse(RecurseOptions),
411 412 413 414 415 416 417 418 419 420
}

impl DirAction {
    pub fn deduce(matches: &getopts::Matches) -> Result<DirAction, Misfire> {
        let recurse = matches.opt_present("recurse");
        let list    = matches.opt_present("list-dirs");
        let tree    = matches.opt_present("tree");

        match (recurse, list, tree) {
            (true,  true,  _    ) => Err(Misfire::Conflict("recurse", "list-dirs")),
B
Ben S 已提交
421
            (_,     true,  true ) => Err(Misfire::Conflict("tree", "list-dirs")),
422 423
            (true,  false, false) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, false)))),
            (_   ,  _,     true ) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, true)))),
424 425 426
            (false, true,  _    ) => Ok(DirAction::AsFile),
            (false, false, _    ) => Ok(DirAction::List),
        }
427
    }
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

    pub fn recurse_options(&self) -> Option<RecurseOptions> {
        match *self {
            DirAction::Recurse(opts) => Some(opts),
            _ => None,
        }
    }

    pub fn is_tree(&self) -> bool {
        match *self {
            DirAction::Recurse(RecurseOptions { max_depth: _, tree }) => tree,
            _ => false,
         }
    }

    pub fn is_recurse(&self) -> bool {
        match *self {
            DirAction::Recurse(RecurseOptions { max_depth: _, tree }) => !tree,
            _ => false,
         }
    }
}

#[derive(PartialEq, Debug, Copy)]
pub struct RecurseOptions {
    pub tree:      bool,
    pub max_depth: Option<usize>,
}

impl RecurseOptions {
    pub fn deduce(matches: &getopts::Matches, tree: bool) -> Result<RecurseOptions, Misfire> {
        let max_depth = if let Some(level) = matches.opt_str("level") {
            match level.parse() {
                Ok(l)  => Some(l),
                Err(e) => return Err(Misfire::FailedParse(e)),
            }
        }
        else {
            None
        };

        Ok(RecurseOptions {
            tree: tree,
            max_depth: max_depth,
        })
    }

    pub fn is_too_deep(&self, depth: usize) -> bool {
        match self.max_depth {
            None    => false,
            Some(d) => {
                d <= depth
            }
        }
    }
483 484
}

B
Ben S 已提交
485 486 487
#[derive(PartialEq, Copy, Debug)]
pub struct Columns {
    size_format: SizeFormat,
488
    time_types: TimeTypes,
B
Ben S 已提交
489 490 491 492 493
    inode: bool,
    links: bool,
    blocks: bool,
    group: bool,
}
B
Ben S 已提交
494

B
Ben S 已提交
495
impl Columns {
496
    pub fn deduce(matches: &getopts::Matches) -> Result<Columns, Misfire> {
B
Ben S 已提交
497
        Ok(Columns {
498
            size_format: try!(SizeFormat::deduce(matches)),
499
            time_types:  try!(TimeTypes::deduce(matches)),
B
Ben S 已提交
500 501 502 503 504
            inode:  matches.opt_present("inode"),
            links:  matches.opt_present("links"),
            blocks: matches.opt_present("blocks"),
            group:  matches.opt_present("group"),
        })
B
Ben S 已提交
505 506
    }

B
Ben S 已提交
507 508
    pub fn for_dir(&self, dir: Option<&Dir>) -> Vec<Column> {
        let mut columns = vec![];
509

B
Ben S 已提交
510 511 512
        if self.inode {
            columns.push(Inode);
        }
B
Ben S 已提交
513

B
Ben S 已提交
514
        columns.push(Permissions);
515

B
Ben S 已提交
516 517 518
        if self.links {
            columns.push(HardLinks);
        }
519

B
Ben S 已提交
520
        columns.push(FileSize(self.size_format));
521

B
Ben S 已提交
522 523 524
        if self.blocks {
            columns.push(Blocks);
        }
525

B
Ben S 已提交
526 527 528 529 530 531
        columns.push(User);

        if self.group {
            columns.push(Group);
        }

B
Ben S 已提交
532 533
        let current_year = LocalDateTime::now().year();

534
        if self.time_types.modified {
B
Ben S 已提交
535
            columns.push(Timestamp(TimeType::FileModified, current_year));
536 537 538
        }

        if self.time_types.created {
B
Ben S 已提交
539
            columns.push(Timestamp(TimeType::FileCreated, current_year));
540 541 542
        }

        if self.time_types.accessed {
B
Ben S 已提交
543
            columns.push(Timestamp(TimeType::FileAccessed, current_year));
544
        }
545

B
Ben S 已提交
546 547 548 549 550 551 552
        if cfg!(feature="git") {
            if let Some(d) = dir {
                if d.has_git_repo() {
                    columns.push(GitStatus);
                }
            }
        }
553

B
Ben S 已提交
554 555
        columns
    }
B
Ben S 已提交
556
}
557 558 559 560

#[cfg(test)]
mod test {
    use super::Options;
B
Ben S 已提交
561 562
    use super::Misfire;
    use super::Misfire::*;
N
nwin 已提交
563
    use xattr;
564

565
    fn is_helpful<T>(misfire: Result<T, Misfire>) -> bool {
B
Ben S 已提交
566
        match misfire {
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
            Err(Help(_)) => true,
            _            => false,
        }
    }

    #[test]
    fn help() {
        let opts = Options::getopts(&[ "--help".to_string() ]);
        assert!(is_helpful(opts))
    }

    #[test]
    fn help_with_file() {
        let opts = Options::getopts(&[ "--help".to_string(), "me".to_string() ]);
        assert!(is_helpful(opts))
    }

    #[test]
    fn files() {
586
        let args = Options::getopts(&[ "this file".to_string(), "that file".to_string() ]).unwrap().1;
B
Ben S 已提交
587
        assert_eq!(args, vec![ "this file".to_string(), "that file".to_string() ])
588 589 590 591
    }

    #[test]
    fn no_args() {
592
        let args = Options::getopts(&[]).unwrap().1;
B
Ben S 已提交
593
        assert_eq!(args, vec![ ".".to_string() ])
594 595 596
    }

    #[test]
597 598
    fn file_sizes() {
        let opts = Options::getopts(&[ "--long".to_string(), "--binary".to_string(), "--bytes".to_string() ]);
B
Ben S 已提交
599
        assert_eq!(opts.unwrap_err(), Misfire::Conflict("binary", "bytes"))
600 601 602 603 604
    }

    #[test]
    fn just_binary() {
        let opts = Options::getopts(&[ "--binary".to_string() ]);
B
Ben S 已提交
605
        assert_eq!(opts.unwrap_err(), Misfire::Useless("binary", false, "long"))
606
    }
607 608 609 610

    #[test]
    fn just_bytes() {
        let opts = Options::getopts(&[ "--bytes".to_string() ]);
B
Ben S 已提交
611
        assert_eq!(opts.unwrap_err(), Misfire::Useless("bytes", false, "long"))
612 613 614 615 616
    }

    #[test]
    fn long_across() {
        let opts = Options::getopts(&[ "--long".to_string(), "--across".to_string() ]);
B
Ben S 已提交
617
        assert_eq!(opts.unwrap_err(), Misfire::Useless("across", true, "long"))
618 619 620 621 622
    }

    #[test]
    fn oneline_across() {
        let opts = Options::getopts(&[ "--oneline".to_string(), "--across".to_string() ]);
B
Ben S 已提交
623
        assert_eq!(opts.unwrap_err(), Misfire::Useless("across", true, "oneline"))
624
    }
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

    #[test]
    fn just_header() {
        let opts = Options::getopts(&[ "--header".to_string() ]);
        assert_eq!(opts.unwrap_err(), Misfire::Useless("header", false, "long"))
    }

    #[test]
    fn just_inode() {
        let opts = Options::getopts(&[ "--inode".to_string() ]);
        assert_eq!(opts.unwrap_err(), Misfire::Useless("inode", false, "long"))
    }

    #[test]
    fn just_links() {
        let opts = Options::getopts(&[ "--links".to_string() ]);
        assert_eq!(opts.unwrap_err(), Misfire::Useless("links", false, "long"))
    }

    #[test]
    fn just_blocks() {
        let opts = Options::getopts(&[ "--blocks".to_string() ]);
        assert_eq!(opts.unwrap_err(), Misfire::Useless("blocks", false, "long"))
    }
B
Ben S 已提交
649

N
nwin 已提交
650 651
    #[test]
    fn extended_without_long() {
N
nwin 已提交
652 653 654 655
        if xattr::feature_implemented() {
            let opts = Options::getopts(&[ "--extended".to_string() ]);
            assert_eq!(opts.unwrap_err(), Misfire::Useless("extended", false, "long"))
        }
N
nwin 已提交
656
    }
B
Ben S 已提交
657 658 659 660 661 662 663

    #[test]
    fn level_without_recurse_or_tree() {
        let opts = Options::getopts(&[ "--level".to_string(), "69105".to_string() ]);
        assert_eq!(opts.unwrap_err(), Misfire::Useless2("level", "recurse", "tree"))
    }

664
}