1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2026 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::{ArgAction, value_parser};
19

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

            
24
51
pub fn cookies() -> clap::Arg {
25
51
    clap::Arg::new("cookies")
26
51
        .long("cookie")
27
51
        .short('b')
28
51
        .value_name("NAME1=VALUE1; NAME2=VALUE2")
29
51
        .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
51
        .action(ArgAction::Append)
53
51
        .num_args(1)
54
}
55

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

            
65
51
pub fn data_raw() -> clap::Arg {
66
51
    clap::Arg::new("data_raw")
67
51
        .long("data-raw")
68
51
        .value_name("data")
69
51
        .overrides_with("data")
70
51
        .num_args(1)
71
}
72

            
73
51
pub fn digest() -> clap::Arg {
74
51
    clap::Arg::new("digest").long("digest").num_args(0)
75
}
76

            
77
51
pub fn headers() -> clap::Arg {
78
51
    clap::Arg::new("headers")
79
51
        .long("header")
80
51
        .short('H')
81
51
        .value_name("NAME:VALUE")
82
60
        .value_parser(|value: &str| {
83
            // We add a basic format check on headers, accepting either "NAME: VALUE" or "NAME;" for an empty header.
84
            // See curl manual <https://curl.se/docs/manpage.html#-H>
85
            // > If you send the custom header with no-value then its header must be terminated with a semicolon,
86
            // > such as -H "X-Custom-Header;" to send "X-Custom-Header:".
87
27
            if value.contains(":") || value.ends_with(";") {
88
27
                Ok(String::from(value))
89
            } else {
90
                Err("headers must be formatted as '<NAME:VALUE>' or '<NAME>;'")
91
            }
92
27
        })
93
51
        .action(ArgAction::Append)
94
51
        .num_args(1)
95
}
96

            
97
51
pub fn insecure() -> clap::Arg {
98
51
    clap::Arg::new("insecure")
99
51
        .long("insecure")
100
51
        .short('k')
101
51
        .num_args(0)
102
}
103

            
104
51
pub fn location() -> clap::Arg {
105
51
    clap::Arg::new("location")
106
51
        .long("location")
107
51
        .short('L')
108
51
        .num_args(0)
109
}
110

            
111
51
pub fn max_redirects() -> clap::Arg {
112
51
    clap::Arg::new("max_redirects")
113
51
        .long("max-redirs")
114
51
        .value_name("NUM")
115
51
        .allow_hyphen_values(true)
116
51
        .value_parser(value_parser!(i32).range(-1..))
117
51
        .num_args(1)
118
}
119

            
120
51
pub fn method() -> clap::Arg {
121
51
    clap::Arg::new("method")
122
51
        .long("request")
123
51
        .short('X')
124
51
        .value_name("METHOD")
125
51
        .num_args(1)
126
}
127

            
128
51
pub fn negotiate() -> clap::Arg {
129
51
    clap::Arg::new("negotiate").long("negotiate").num_args(0)
130
}
131

            
132
51
pub fn ntlm() -> clap::Arg {
133
51
    clap::Arg::new("ntlm").long("ntlm").num_args(0)
134
}
135

            
136
51
pub fn retry() -> clap::Arg {
137
51
    clap::Arg::new("retry")
138
51
        .long("retry")
139
51
        .value_name("seconds")
140
51
        .value_parser(value_parser!(i32))
141
51
        .num_args(1)
142
}
143

            
144
51
pub fn url() -> clap::Arg {
145
51
    clap::Arg::new("url")
146
51
        .long("url")
147
51
        .value_name("url")
148
51
        .num_args(1)
149
}
150

            
151
51
pub fn url_param() -> clap::Arg {
152
51
    clap::Arg::new("url_param")
153
51
        .help("Sets the url to use")
154
51
        .required(false)
155
51
        .num_args(1)
156
}
157

            
158
51
pub fn user() -> clap::Arg {
159
51
    clap::Arg::new("user").long("user").short('u').num_args(1)
160
}
161

            
162
51
pub fn verbose() -> clap::Arg {
163
51
    clap::Arg::new("verbose")
164
51
        .long("verbose")
165
51
        .short('v')
166
51
        .num_args(0)
167
}