1// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu> 2// 3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 6// option. This file may not be copied, modified, or distributed 7// except according to those terms. 8 9use clap::Parser; 10 11#[test] 12fn skip_1() { 13 #[derive(Parser, Debug, PartialEq)] 14 struct Opt { 15 #[arg(short)] 16 x: u32, 17 #[arg(skip)] 18 s: u32, 19 } 20 21 assert!(Opt::try_parse_from(["test", "-x", "10", "20"]).is_err()); 22 23 let mut opt = Opt::try_parse_from(["test", "-x", "10"]).unwrap(); 24 assert_eq!( 25 opt, 26 Opt { 27 x: 10, 28 s: 0, // default 29 } 30 ); 31 opt.s = 42; 32 33 opt.try_update_from(["test", "-x", "22"]).unwrap(); 34 35 assert_eq!(opt, Opt { x: 22, s: 42 }); 36} 37 38#[test] 39fn skip_2() { 40 #[derive(Parser, Debug, PartialEq)] 41 struct Opt { 42 #[arg(short)] 43 x: u32, 44 #[arg(skip)] 45 ss: String, 46 #[arg(skip)] 47 sn: u8, 48 49 y: u32, 50 #[arg(skip)] 51 sz: u16, 52 53 t: u32, 54 } 55 56 assert_eq!( 57 Opt::try_parse_from(["test", "-x", "10", "20", "30"]).unwrap(), 58 Opt { 59 x: 10, 60 ss: String::from(""), 61 sn: 0, 62 y: 20, 63 sz: 0, 64 t: 30, 65 } 66 ); 67} 68 69#[test] 70fn skip_enum() { 71 #[derive(Debug, PartialEq)] 72 #[allow(unused)] 73 enum Kind { 74 A, 75 B, 76 } 77 78 impl Default for Kind { 79 fn default() -> Self { 80 Kind::B 81 } 82 } 83 84 #[derive(Parser, Debug, PartialEq)] 85 pub struct Opt { 86 #[arg(long, short)] 87 number: u32, 88 #[arg(skip)] 89 k: Kind, 90 #[arg(skip)] 91 v: Vec<u32>, 92 } 93 94 assert_eq!( 95 Opt::try_parse_from(["test", "-n", "10"]).unwrap(), 96 Opt { 97 number: 10, 98 k: Kind::B, 99 v: vec![], 100 } 101 ); 102} 103 104#[test] 105fn skip_help_doc_comments() { 106 #[derive(Parser, Debug, PartialEq, Eq)] 107 pub struct Opt { 108 #[arg(skip, help = "internal_stuff")] 109 a: u32, 110 111 #[arg(skip, long_help = "internal_stuff\ndo not touch")] 112 b: u32, 113 114 /// Not meant to be used by clap. 115 /// 116 /// I want a default here. 117 #[arg(skip)] 118 c: u32, 119 120 #[arg(short)] 121 n: u32, 122 } 123 124 assert_eq!( 125 Opt::try_parse_from(["test", "-n", "10"]).unwrap(), 126 Opt { 127 n: 10, 128 a: 0, 129 b: 0, 130 c: 0, 131 } 132 ); 133} 134 135#[test] 136fn skip_val() { 137 #[derive(Parser, Debug, PartialEq, Eq)] 138 pub struct Opt { 139 #[arg(long, short)] 140 number: u32, 141 142 #[arg(skip = "key")] 143 k: String, 144 145 #[arg(skip = vec![1, 2, 3])] 146 v: Vec<u32>, 147 } 148 149 assert_eq!( 150 Opt::try_parse_from(["test", "-n", "10"]).unwrap(), 151 Opt { 152 number: 10, 153 k: "key".to_string(), 154 v: vec![1, 2, 3] 155 } 156 ); 157} 158