1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use clap::{value_parser, ArgAction};
19

            
20
45
pub fn compressed() -> clap::Arg {
21
45
    clap::Arg::new("compressed").long("compressed").num_args(0)
22
}
23

            
24
45
pub fn cookies() -> clap::Arg {
25
45
    clap::Arg::new("cookies")
26
45
        .long("cookie")
27
45
        .short('b')
28
45
        .value_name("NAME1=VALUE1; NAME2=VALUE2")
29
45
        .value_parser(|value: &str| {
30
            if value.trim().is_empty() {
31
                return Err("empty value provided".to_string());
32
            }
33

            
34
            let (valid_cookies, invalid_cookies): (Vec<_>, Vec<_>) = value
35
                .split(';')
36
                .map(str::trim)
37
                .filter(|c| !c.is_empty())
38
                .partition(|c| c.contains('='));
39

            
40
            if invalid_cookies.is_empty() {
41
                Ok(valid_cookies.join("; "))
42
            } else {
43
                match invalid_cookies.as_slice() {
44
                    [_] => Err("invalid cookie pair provided".to_string()),
45
                    _ => Err(format!(
46
                        "invalid cookie pairs provided: [{}]",
47
                        invalid_cookies.join(", ")
48
                    )),
49
                }
50
            }
51
        })
52
45
        .action(ArgAction::Append)
53
45
        .num_args(1)
54
}
55

            
56
45
pub fn data() -> clap::Arg {
57
45
    clap::Arg::new("data")
58
45
        .long("data")
59
45
        .short('d')
60
45
        .value_name("data")
61
45
        .num_args(1)
62
}
63

            
64
45
pub fn headers() -> clap::Arg {
65
45
    clap::Arg::new("headers")
66
45
        .long("header")
67
45
        .short('H')
68
45
        .value_name("NAME:VALUE")
69
53
        .value_parser(|value: &str| {
70
            // We add a basic format check on headers, accepting either "NAME: VALUE" or "NAME;" for an empty header.
71
            // See curl manual <https://curl.se/docs/manpage.html#-H>
72
            // > If you send the custom header with no-value then its header must be terminated with a semicolon,
73
            // > such as -H "X-Custom-Header;" to send "X-Custom-Header:".
74
24
            if value.contains(":") || value.ends_with(";") {
75
24
                Ok(String::from(value))
76
            } else {
77
                Err("headers must be formatted as '<NAME:VALUE>' or '<NAME>;'")
78
            }
79
24
        })
80
45
        .action(ArgAction::Append)
81
45
        .num_args(1)
82
}
83

            
84
45
pub fn insecure() -> clap::Arg {
85
45
    clap::Arg::new("insecure")
86
45
        .long("insecure")
87
45
        .short('k')
88
45
        .num_args(0)
89
}
90

            
91
45
pub fn location() -> clap::Arg {
92
45
    clap::Arg::new("location")
93
45
        .long("location")
94
45
        .short('L')
95
45
        .num_args(0)
96
}
97

            
98
45
pub fn max_redirects() -> clap::Arg {
99
45
    clap::Arg::new("max_redirects")
100
45
        .long("max-redirs")
101
45
        .value_name("NUM")
102
45
        .allow_hyphen_values(true)
103
45
        .value_parser(value_parser!(i32).range(-1..))
104
45
        .num_args(1)
105
}
106

            
107
45
pub fn method() -> clap::Arg {
108
45
    clap::Arg::new("method")
109
45
        .long("request")
110
45
        .short('X')
111
45
        .value_name("METHOD")
112
45
        .num_args(1)
113
}
114

            
115
45
pub fn negotiate() -> clap::Arg {
116
45
    clap::Arg::new("negotiate").long("negotiate").num_args(0)
117
}
118

            
119
45
pub fn ntlm() -> clap::Arg {
120
45
    clap::Arg::new("ntlm").long("ntlm").num_args(0)
121
}
122

            
123
45
pub fn retry() -> clap::Arg {
124
45
    clap::Arg::new("retry")
125
45
        .long("retry")
126
45
        .value_name("seconds")
127
45
        .value_parser(value_parser!(i32))
128
45
        .num_args(1)
129
}
130

            
131
45
pub fn url() -> clap::Arg {
132
45
    clap::Arg::new("url")
133
45
        .long("url")
134
45
        .value_name("url")
135
45
        .num_args(1)
136
}
137

            
138
45
pub fn url_param() -> clap::Arg {
139
45
    clap::Arg::new("url_param")
140
45
        .help("Sets the url to use")
141
45
        .required(false)
142
45
        .num_args(1)
143
}
144

            
145
45
pub fn user() -> clap::Arg {
146
45
    clap::Arg::new("user").long("user").short('u').num_args(1)
147
}
148

            
149
45
pub fn verbose() -> clap::Arg {
150
45
    clap::Arg::new("verbose")
151
45
        .long("verbose")
152
45
        .short('v')
153
45
        .num_args(0)
154
}