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

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

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

            
64
33
pub fn headers() -> clap::Arg {
65
33
    clap::Arg::new("headers")
66
33
        .long("header")
67
33
        .short('H')
68
33
        .value_name("NAME:VALUE")
69
41
        .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
33
        .action(ArgAction::Append)
81
33
        .num_args(1)
82
}
83

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

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

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

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

            
115
33
pub fn retry() -> clap::Arg {
116
33
    clap::Arg::new("retry")
117
33
        .long("retry")
118
33
        .value_name("seconds")
119
33
        .value_parser(value_parser!(i32))
120
33
        .num_args(1)
121
}
122

            
123
33
pub fn url() -> clap::Arg {
124
33
    clap::Arg::new("url")
125
33
        .long("url")
126
33
        .value_name("url")
127
33
        .num_args(1)
128
}
129

            
130
33
pub fn url_param() -> clap::Arg {
131
33
    clap::Arg::new("url_param")
132
33
        .help("Sets the url to use")
133
33
        .required(false)
134
33
        .num_args(1)
135
}
136

            
137
33
pub fn verbose() -> clap::Arg {
138
33
    clap::Arg::new("verbose")
139
33
        .long("verbose")
140
33
        .short('v')
141
33
        .num_args(0)
142
}