1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
crate::ix!();

impl ArgsManagerInner {
    
    pub fn parse_parameters(
        &mut self, 
        argv:  &Vec<String>,
        error: &mut String) -> bool {

        let argc = argv.len();

        //LOCK(cs_args);
        
        self.settings.command_line_options.clear();

        for mut i in 1..argc {

            let mut key: String = argv[i as usize].to_string();

            #[cfg(MAC_OSX)]
            {
                // At the first time when a user
                // gets the "App downloaded from
                // the internet" warning, and
                // clicks the Open button, macOS
                // passes a unique process serial
                // number (PSN) as
                // -psn_... command-line argument,
                // which we filter out.
                if key.substr(0, 5) == "-psn_" {
                    continue;
                }
            }

            // bitcoin-tx using stdin
            if key == "-" {
                break;
            }

            let mut val = String::default();

            if let Some(is_index) = key.find('=') {

                val = key[is_index + 1..].to_string();

                key.remove(is_index);
            }

            #[cfg(WIN32)]
            {
                key = to_lower(key);

                if key[0] == '/' {
                    key[0] = '-';
                }
            }

            if key.chars().nth(0) != Some('-') {

                if !self.accept_any_command && self.command.is_empty() {

                    // The first non-dash arg is a registered command
                    let flags: Option::<u32> = self.get_arg_flags(&key);

                    if flags.is_none() || (flags.unwrap() & ArgsManagerFlags::COMMAND.bits()) == 0 {

                        *error = format!{
                            "Invalid command '{}'",
                            argv[i as usize]
                        };

                        return false;
                    }
                }

                self.command.push(key);

                while {
                    i += 1;
                    i
                } < argc{
                    //  The remaining args are command args
                    self.command.push(argv[i as usize].to_string());
                }

                break;
            }

            // Transform --foo to -foo
            if key.len() > 1 && key.chars().nth(1).unwrap() == '-' {
                key = key[1..].to_string();
            }

            //  Transform -foo to foo
            key = key[1..].to_string();

            let mut section = String::default();

            let arg = format!{"-{}",key};

            let value: SettingsValue = interpret_option(&mut section,&mut key,&val);

            let flags: Option::<u32> = self.get_arg_flags(&arg);

            // Unknown command line options and
            // command line options with dot
            // characters (which are returned from
            // InterpretOption with nonempty
            // section strings) are not valid.
            if flags.is_none() || section.is_empty() {
                *error = format!{"Invalid parameter {:?}",argv[i as usize]};
                return false;
            }

            if !check_valid(&key,&value,flags.unwrap(),error) {
                return false;
            }

            self.settings
                .command_line_options
                .get_mut(&key)
                .unwrap()
                .push(value);
        }

        // we do not allow -includeconf from
        // command line, only -noincludeconf
        let includes = self.settings.command_line_options.get( "includeconf");

        if includes.is_some() {

            let values: SettingsSpan = SettingsSpan::from(includes.unwrap());

            //  Range may be empty if -noincludeconf was passed
            if !values.empty() {

                *error = format!{
                    "-includeconf cannot be used from commandline; -includeconf={}", 
                    unsafe { (*values.begin()).0.write(None, None) }
                };

                // pick first value as example
                return false;
            }
        }

        true
    }
}