forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.nu
74 lines (61 loc) · 1.92 KB
/
flag.nu
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
### So in this case you have to pass in a parameter
### Any parameter you type will work
### If you don't type a parameter you get an error
###
### The syntax for this is
### noflag hola
###
def noflag [x] {
echo $x
}
### The syntax for this is
### flag -f
### flag --flag
### If you type anything else it does not work
### For example
### flag -flag
### flag -f=hola
### flag -f hola
### flag -f = hola
def flag [
--flag(-f)
] {
echo $flag
}
# Write out the flags you entered
def flag_details [myint: int, mystring: string] {
echo "myint is " $myint | str join
echo "mystring is " $mystring | str join
}
# Get the data passed into the flags
def get_flag [
--test_int(-i): int # The test intlocation
--test_string(-s): string # The test string
] {
let is_int_empty = ($test_int == null)
let is_string_empty = ($test_string == null)
let no_int_no_string = ($is_int_empty == true and $is_string_empty == true)
let no_int_with_string = ($is_int_empty == true and $is_string_empty == false)
let with_int_no_string = ($is_int_empty == false and $is_string_empty == true)
let with_int_with_string = ($is_int_empty == false and $is_string_empty == false)
echo 'no int and no string ' $no_int_no_string | str join
echo 'no int with string ' $no_int_with_string | str join
echo 'with int and no string ' $with_int_no_string | str join
echo 'with int and with string ' $with_int_with_string | str join
if $no_int_no_string {
(flag_details 1 "blue")
} else if $no_int_with_string {
(flag_details 1 $test_string)
} else if $with_int_no_string {
(flag_details $test_int "blue")
} else if $with_int_with_string {
(flag_details $test_int $test_string)
}
}
# To run this call
# > get_flag
# it will default to int 1 and string blue
# > get_flag -i 2
# This changes to int 2 and string blue
# > get_flag -i 3 -s green
# This changes to int 3 and string green