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 data() -> clap::Arg {
25
33
    clap::Arg::new("data")
26
33
        .long("data")
27
33
        .short('d')
28
33
        .value_name("data")
29
33
        .num_args(1)
30
}
31

            
32
33
pub fn headers() -> clap::Arg {
33
33
    clap::Arg::new("headers")
34
33
        .long("header")
35
33
        .short('H')
36
33
        .value_name("NAME:VALUE")
37
41
        .value_parser(|value: &str| {
38
24
            // We add a basic format check on headers, accepting either "NAME: VALUE" or "NAME;" for an empty header.
39
24
            // See curl manual <https://curl.se/docs/manpage.html#-H>
40
24
            // > If you send the custom header with no-value then its header must be terminated with a semicolon,
41
24
            // > such as -H "X-Custom-Header;" to send "X-Custom-Header:".
42
24
            if value.contains(":") || value.ends_with(";") {
43
24
                Ok(String::from(value))
44
            } else {
45
                Err("headers must be formatted as '<NAME:VALUE>' or '<NAME>;'")
46
            }
47
41
        })
48
33
        .action(ArgAction::Append)
49
33
        .num_args(1)
50
}
51

            
52
33
pub fn insecure() -> clap::Arg {
53
33
    clap::Arg::new("insecure")
54
33
        .long("insecure")
55
33
        .short('k')
56
33
        .num_args(0)
57
}
58

            
59
33
pub fn location() -> clap::Arg {
60
33
    clap::Arg::new("location")
61
33
        .long("location")
62
33
        .short('L')
63
33
        .num_args(0)
64
}
65

            
66
33
pub fn max_redirects() -> clap::Arg {
67
33
    clap::Arg::new("max_redirects")
68
33
        .long("max-redirs")
69
33
        .value_name("NUM")
70
33
        .allow_hyphen_values(true)
71
33
        .value_parser(value_parser!(i32).range(-1..))
72
33
        .num_args(1)
73
}
74

            
75
33
pub fn method() -> clap::Arg {
76
33
    clap::Arg::new("method")
77
33
        .long("request")
78
33
        .short('X')
79
33
        .value_name("METHOD")
80
33
        .num_args(1)
81
}
82

            
83
33
pub fn retry() -> clap::Arg {
84
33
    clap::Arg::new("retry")
85
33
        .long("retry")
86
33
        .value_name("seconds")
87
33
        .value_parser(value_parser!(i32))
88
33
        .num_args(1)
89
}
90

            
91
33
pub fn url() -> clap::Arg {
92
33
    clap::Arg::new("url")
93
33
        .long("url")
94
33
        .value_name("url")
95
33
        .num_args(1)
96
}
97

            
98
33
pub fn url_param() -> clap::Arg {
99
33
    clap::Arg::new("url_param")
100
33
        .help("Sets the url to use")
101
33
        .required(false)
102
33
        .num_args(1)
103
}
104

            
105
33
pub fn verbose() -> clap::Arg {
106
33
    clap::Arg::new("verbose")
107
33
        .long("verbose")
108
33
        .short('v')
109
33
        .num_args(0)
110
}