cliutil 0.1.0-alpha.0

A command line utilities library
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
 * Copyright 2023 Jacob R. Green
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::cell::{Cell, UnsafeCell};
use std::env;
use std::fmt::Display;

type Command<'a, R> = &'a (dyn Fn() -> R + Sync);

/*
 * Flags
 */

pub struct FlagValue {
    value: Cell<bool>,
}

impl FlagValue {
    pub const fn new() -> Self {
        Self {
            value: Cell::new(false),
        }
    }

    pub fn value(&self) -> bool {
        self.value.get()
    }

    fn mark(&self) {
        self.value.set(true)
    }
}

unsafe impl Sync for FlagValue {}

/// A command line boolean flag.
///
/// # Example
/// ```bash
/// $ ./myapp -{short_name} --{long_name}
/// ```
pub struct Flag<'a> {
    short_name: &'a str,
    long_name: &'a str,
    description: &'a str,
    flag: &'a FlagValue,
}

impl<'a> Flag<'a> {
    pub const fn build() -> FlagBuilder<'a> {
        FlagBuilder {
            short_name: None,
            long_name: None,
            description: None,
            flag: None,
        }
    }

    fn mark(&self) {
        self.flag.mark();
    }

    pub const fn short_name(&self) -> &str {
        self.short_name
    }

    pub const fn long_name(&self) -> &str {
        self.long_name
    }

    pub const fn description(&self) -> &str {
        self.description
    }
}

pub struct FlagBuilder<'a> {
    short_name: Option<&'a str>,
    long_name: Option<&'a str>,
    description: Option<&'a str>,
    flag: Option<&'a FlagValue>,
}

impl<'a> FlagBuilder<'a> {
    pub const fn with_short_name(mut self, short_name: &'a str) -> Self {
        self.short_name = Some(short_name);
        self
    }

    pub const fn with_long_name(mut self, long_name: &'a str) -> Self {
        self.long_name = Some(long_name);
        self
    }

    pub const fn with_description(mut self, description: &'a str) -> Self {
        self.description = Some(description);
        self
    }

    pub const fn with_flag(mut self, flag: &'a FlagValue) -> Self {
        self.flag = Some(flag);
        self
    }

    pub const fn build(self) -> Flag<'a> {
        let flag = Flag {
            short_name: match self.short_name {
                Some(short_name) => short_name,
                None => "",
            },
            long_name: match self.long_name {
                Some(long_name) => long_name,
                None => "",
            },
            description: match self.description {
                Some(description) => description,
                None => "",
            },
            flag: match self.flag {
                Some(flag) => flag,
                None => panic!("Flag must have a flag value."),
            },
        };
        assert!(
            !(flag.short_name.is_empty() && flag.long_name.is_empty()),
            "Flag must at least have a short name or long name."
        );
        flag
    }
}

pub struct ParameterValue {
    value: UnsafeCell<Option<String>>,
}

impl ParameterValue {
    pub const fn new() -> Self {
        Self {
            value: UnsafeCell::new(None),
        }
    }

    pub fn value(&self) -> Option<&str> {
        unsafe { (&*self.value.get()).as_ref().map(|s| s.as_str()) }
    }

    fn set_value(&self, value: String) {
        unsafe {
            self.value.get().replace(Some(value));
        }
    }
}

unsafe impl Sync for ParameterValue {}

/// A command line string parameter.
///
/// # Example
/// ```bash
/// $ ./myapp -{short_name} {value} --{long_name}={value}
/// ```
pub struct Parameter<'a> {
    short_name: &'a str,
    long_name: &'a str,
    description: &'a str,
    value: &'a ParameterValue,
}

impl<'a> Parameter<'a> {
    pub const fn build() -> ParameterBuilder<'a> {
        ParameterBuilder {
            short_name: None,
            long_name: None,
            description: None,
            parameter: None,
        }
    }

    fn set_value(&self, value: String) {
        self.value.set_value(value);
    }

    pub const fn short_name(&self) -> &str {
        self.short_name
    }

    pub const fn long_name(&self) -> &str {
        self.long_name
    }

    pub const fn description(&self) -> &str {
        self.description
    }
}

pub struct ParameterBuilder<'a> {
    short_name: Option<&'a str>,
    long_name: Option<&'a str>,
    description: Option<&'a str>,
    parameter: Option<&'a ParameterValue>,
}

impl<'a> ParameterBuilder<'a> {
    pub const fn with_short_name(mut self, short_name: &'a str) -> Self {
        self.short_name = Some(short_name);
        self
    }

    pub const fn with_long_name(mut self, long_name: &'a str) -> Self {
        self.long_name = Some(long_name);
        self
    }

    pub const fn with_description(mut self, description: &'a str) -> Self {
        self.description = Some(description);
        self
    }

    pub const fn with_parameter(mut self, value: &'a ParameterValue) -> Self {
        self.parameter = Some(value);
        self
    }

    pub const fn build(self) -> Parameter<'a> {
        let param = Parameter {
            short_name: match self.short_name {
                Some(short_name) => short_name,
                None => "",
            },
            long_name: match self.long_name {
                Some(long_name) => long_name,
                None => "",
            },
            description: match self.description {
                Some(description) => description,
                None => "",
            },
            value: match self.parameter {
                Some(value) => value,
                None => panic!("Parameter must have a value."),
            },
        };
        assert!(
            !(param.short_name.is_empty() && param.long_name.is_empty()),
            "Parameter must at least have a short name or long name."
        );
        param
    }
}

/// A command line executable.
trait Executable<R> {
    fn flags(&self) -> &[Flag];
    fn parameters(&self) -> &[Parameter];
    fn subcommands(&self) -> &[SubCommand<R>];
    fn command(&self) -> Option<Command<R>>;

    fn execute<T: AsRef<str> + Display>(&self, mut args: impl Iterator<Item = T>) -> R {
        let flags = self.flags();
        let params = self.parameters();
        let subcommands = self.subcommands();
        let command = self.command();

        while let Some(arg) = args.next() {
            // long name
            if arg.as_ref().starts_with("--") {
                let arg_slice = &arg.as_ref()[2..];

                // parameter
                if let Some(equals_pos) = arg_slice.find('=') {
                    let param_name = &arg_slice[0..equals_pos];
                    let param_value = &arg_slice[equals_pos + 1..];

                    if let Some(param) = params.iter().find(|param| param.long_name == param_name) {
                        param.set_value(param_value.to_string())
                    } else {
                        panic!("Unexpected parameter: \"{}\"", arg)
                    }
                }
                // flag
                else {
                    if let Some(flag) = flags.iter().find(|flag| flag.long_name == arg_slice) {
                        flag.mark()
                    } else {
                        panic!("Unexpected flag: \"{}\"", arg)
                    }
                }
            }
            // short name
            else if arg.as_ref().starts_with("-") {
                let arg_slice = &arg.as_ref()[1..];

                // flag
                if let Some(flag) = flags.iter().find(|flag| flag.short_name == arg_slice) {
                    flag.mark()
                } else if let Some(param) =
                    params.iter().find(|param| param.short_name == arg_slice)
                {
                    if let Some(param_value) = args.next() {
                        param.set_value(param_value.to_string())
                    } else {
                        panic!("Expected value for parameter: \"{}\"", arg)
                    }
                } else {
                    panic!("Unexpected flag: \"{}\"", arg)
                }
            }
            // command
            else {
                if let Some(command) = subcommands
                    .iter()
                    .find(|command| command.long_name == arg.as_ref())
                {
                    return command.execute(args);
                } else {
                    panic!("Unexpected command: \"{}\"", arg)
                }
            }
        }

        if let Some(command) = command {
            return command();
        } else {
            // todo - subcommand required
            panic!("");
        }
    }
}

/// A command line subcommand.
///
/// # Example
/// ```bash
/// $ ./myapp {subcommand} --{flag} --{parameter}={value}
/// ```
pub struct SubCommand<'a, R = ()> {
    //short_name: &'a str,
    long_name: &'a str,
    description: &'a str,
    flags: &'a [Flag<'a>],
    params: &'a [Parameter<'a>],
    subcommands: &'a [SubCommand<'a, R>],
    command: Option<Command<'a, R>>,
}

impl<'a, R> SubCommand<'a, R> {
    pub const fn build() -> SubCommandBuilder<'a, R> {
        SubCommandBuilder {
            long_name: None,
            description: None,
            flags: None,
            params: None,
            subcommands: None,
            command: None,
        }
    }

    pub const fn long_name(&self) -> &str {
        self.long_name
    }

    pub const fn description(&self) -> &str {
        self.description
    }

    pub const fn flags(&self) -> &[Flag] {
        self.flags
    }

    pub const fn parameters(&self) -> &[Parameter] {
        self.params
    }

    pub const fn subcommands(&self) -> &[SubCommand<R>] {
        self.subcommands
    }
}

pub struct SubCommandBuilder<'a, R> {
    //short_name: &'a str,
    long_name: Option<&'a str>,
    description: Option<&'a str>,
    flags: Option<&'a [Flag<'a>]>,
    params: Option<&'a [Parameter<'a>]>,
    subcommands: Option<&'a [SubCommand<'a, R>]>,
    command: Option<Command<'a, R>>,
}

impl<'a, R> SubCommandBuilder<'a, R> {
    pub const fn with_long_name(mut self, long_name: &'a str) -> Self {
        self.long_name = Some(long_name);
        self
    }

    pub const fn with_description(mut self, description: &'a str) -> Self {
        self.description = Some(description);
        self
    }

    pub const fn with_flags(mut self, flags: &'a [Flag<'a>]) -> Self {
        self.flags = Some(flags);
        self
    }

    pub const fn with_parameters(mut self, params: &'a [Parameter<'a>]) -> Self {
        self.params = Some(params);
        self
    }

    pub const fn with_subcommands(mut self, subcommands: &'a [SubCommand<'a, R>]) -> Self {
        self.subcommands = Some(subcommands);
        self
    }

    pub const fn with_command(mut self, command: Command<'a, R>) -> Self {
        self.command = Some(command);
        self
    }

    pub const fn build(self) -> SubCommand<'a, R> {
        let subcommand = SubCommand {
            long_name: match self.long_name {
                Some(long_name) => long_name,
                None => panic!("Subcommand must have a long name."),
            },
            description: match self.description {
                Some(description) => description,
                None => "",
            },
            flags: match self.flags {
                Some(flags) => flags,
                None => &[],
            },
            params: match self.params {
                Some(params) => params,
                None => &[],
            },
            subcommands: match self.subcommands {
                Some(subcommands) => subcommands,
                None => &[],
            },
            command: self.command,
        };

        assert!(
            subcommand.command.is_some() || !subcommand.subcommands.is_empty(),
            "Subcommand must have either a default command or at least one subcommand."
        );

        subcommand
    }
}

impl<R> Executable<R> for SubCommand<'_, R> {
    fn flags(&self) -> &[Flag] {
        self.flags
    }

    fn parameters(&self) -> &[Parameter] {
        self.params
    }

    fn subcommands(&self) -> &[SubCommand<R>] {
        self.subcommands
    }

    fn command(&self) -> Option<Command<R>> {
        self.command
    }
}

/// The root of a console application.
///
/// # Example
/// Application with a default command:
///
/// ```rust
/// use cliutil::constexpr as cli;
///
/// static EXAMPLE_APPLICATION: cli::Application =
///     cli::Application::build()
///         .with_name("Example Cli App")
///         .with_description("An example CLI application")
///         .with_command(&app_main)
///         .build();  
///
/// fn main() {
///     EXAMPLE_APPLICATION.run()
/// }
///  
/// fn app_main() {
///     println!("Hello, world!");
/// }
/// ```
pub struct Application<'a, R = ()> {
    name: &'a str,
    description: &'a str,
    flags: &'a [Flag<'a>],
    params: &'a [Parameter<'a>],
    subcommands: &'a [SubCommand<'a, R>],
    command: Option<Command<'a, R>>,
    // help: bool,
    // version: bool,
}

impl<'a, R> Application<'a, R> {
    pub const fn build() -> ApplicationBuilder<'a, R> {
        ApplicationBuilder {
            name: None,
            description: None,
            flags: None,
            params: None,
            subcommands: None,
            command: None,
            //help: true,
        }
    }

    pub fn run(&self) -> R {
        let mut args = env::args();
        let _binary = args.next();
        self.execute(args)
    }

    pub const fn name(&self) -> &str {
        self.name
    }

    pub const fn description(&self) -> &str {
        self.description
    }

    pub const fn flags(&self) -> &[Flag] {
        self.flags
    }

    pub const fn parameters(&self) -> &[Parameter] {
        self.params
    }

    pub const fn subcommands(&self) -> &[SubCommand<R>] {
        self.subcommands
    }
}

pub struct ApplicationBuilder<'a, R> {
    name: Option<&'a str>,
    description: Option<&'a str>,
    flags: Option<&'a [Flag<'a>]>,
    params: Option<&'a [Parameter<'a>]>,
    subcommands: Option<&'a [SubCommand<'a, R>]>,
    command: Option<Command<'a, R>>,
    // help: bool,
}

impl<'a, R> ApplicationBuilder<'a, R> {
    pub const fn with_name(mut self, name: &'a str) -> Self {
        self.name = Some(name);
        self
    }

    pub const fn with_description(mut self, description: &'a str) -> Self {
        self.description = Some(description);
        self
    }

    pub const fn with_flags(mut self, flags: &'a [Flag<'a>]) -> Self {
        self.flags = Some(flags);
        self
    }

    pub const fn with_parameters(mut self, params: &'a [Parameter<'a>]) -> Self {
        self.params = Some(params);
        self
    }

    pub const fn with_subcommands(mut self, subcommands: &'a [SubCommand<'a, R>]) -> Self {
        self.subcommands = Some(subcommands);
        self
    }

    pub const fn with_command(mut self, command: Command<'a, R>) -> Self {
        self.command = Some(command);
        self
    }

    // pub const fn with_help(mut self, help: bool) -> Self {
    //     self.help = help;
    //     self
    // }

    pub const fn build(self) -> Application<'a, R> {
        let app = Application {
            name: match self.name {
                Some(name) => name,
                None => "",
            },
            description: match self.description {
                Some(description) => description,
                None => "",
            },
            flags: match self.flags {
                Some(flags) => flags,
                None => &[],
            },
            params: match self.params {
                Some(params) => params,
                None => &[],
            },
            subcommands: match self.subcommands {
                Some(subcommands) => subcommands,
                None => &[],
            },
            command: self.command,
            // help: self.help,
        };
        assert!(
            app.command.is_some() || !app.subcommands.is_empty(),
            "Application must have either a default command or at least one subcommand."
        );
        app
    }
}

impl<R> Executable<R> for Application<'_, R> {
    fn flags(&self) -> &[Flag] {
        self.flags
    }

    fn parameters(&self) -> &[Parameter] {
        self.params
    }

    fn subcommands(&self) -> &[SubCommand<R>] {
        self.subcommands
    }

    fn command(&self) -> Option<Command<R>> {
        self.command
    }
}