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::{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 digest() -> clap::Arg {
65
45
    clap::Arg::new("digest").long("digest").num_args(0)
66
}
67

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

            
88
45
pub fn insecure() -> clap::Arg {
89
45
    clap::Arg::new("insecure")
90
45
        .long("insecure")
91
45
        .short('k')
92
45
        .num_args(0)
93
}
94

            
95
45
pub fn location() -> clap::Arg {
96
45
    clap::Arg::new("location")
97
45
        .long("location")
98
45
        .short('L')
99
45
        .num_args(0)
100
}
101

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

            
111
45
pub fn method() -> clap::Arg {
112
45
    clap::Arg::new("method")
113
45
        .long("request")
114
45
        .short('X')
115
45
        .value_name("METHOD")
116
45
        .num_args(1)
117
}
118

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

            
123
45
pub fn ntlm() -> clap::Arg {
124
45
    clap::Arg::new("ntlm").long("ntlm").num_args(0)
125
}
126

            
127
45
pub fn retry() -> clap::Arg {
128
45
    clap::Arg::new("retry")
129
45
        .long("retry")
130
45
        .value_name("seconds")
131
45
        .value_parser(value_parser!(i32))
132
45
        .num_args(1)
133
}
134

            
135
45
pub fn url() -> clap::Arg {
136
45
    clap::Arg::new("url")
137
45
        .long("url")
138
45
        .value_name("url")
139
45
        .num_args(1)
140
}
141

            
142
45
pub fn url_param() -> clap::Arg {
143
45
    clap::Arg::new("url_param")
144
45
        .help("Sets the url to use")
145
45
        .required(false)
146
45
        .num_args(1)
147
}
148

            
149
45
pub fn user() -> clap::Arg {
150
45
    clap::Arg::new("user").long("user").short('u').num_args(1)
151
}
152

            
153
45
pub fn verbose() -> clap::Arg {
154
45
    clap::Arg::new("verbose")
155
45
        .long("verbose")
156
45
        .short('v')
157
45
        .num_args(0)
158
}