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
36
pub fn compressed() -> clap::Arg {
21
36
    clap::Arg::new("compressed").long("compressed").num_args(0)
22
}
23

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

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

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

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

            
77
36
pub fn headers() -> clap::Arg {
78
36
    clap::Arg::new("headers")
79
36
        .long("header")
80
36
        .short('H')
81
36
        .value_name("NAME:VALUE")
82
36
        .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
18
            if value.contains(":") || value.ends_with(";") {
88
18
                Ok(String::from(value))
89
            } else {
90
                Err("headers must be formatted as '<NAME:VALUE>' or '<NAME>;'")
91
            }
92
18
        })
93
36
        .action(ArgAction::Append)
94
36
        .num_args(1)
95
}
96

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

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

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

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

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

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

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

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

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

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

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