about summary refs log tree commit diff
path: root/pkgs/profpatsch/dhallsh/main.dhall
blob: 36be6142137076b5ab29c8a3ee13070a2d8d8c66 (plain) (blame)
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
let Prelude = ./imports/Prelude.dhall

let List/filterOptional = ./List/filterOptional.dhall

let Command = ./Command/type.dhall

let Option = ./Option/type.dhall

let Argument = ./Argument/type.dhall

let Complete =
	  { cmd :
		  Text
	  , description :
		  Text
	  , condition :
		  Optional (Command Argument)
	  , short-option :
		  Optional Text
	  , long-option :
		  Optional Text
	  , long-option-old-style :
		  Optional Text
	  , argument :
		  Optional Text
	  , keep-order :
		  Bool
	  , no-files :
		  Bool
	  , require-parameter :
		  Bool
	  , wraps :
		  Optional Text
	  }

let argCommandToList
	: Command Argument → List Text
	= ./Command/toList.dhall
	  Argument
	  (./Argument/toArgList.dhall ./OptionPrinter/newStyle.dhall)

let complete =
		λ(a : { cmd : Text, description : Text })
	  → { cmd =
			a.cmd
		, description =
			a.description
		, condition =
			None (Command Argument)
		, short-option =
			None Text
		, long-option =
			None Text
		, long-option-old-style =
			None Text
		, argument =
			None Text
		, keep-order =
			False
		, no-files =
			True
		, require-parameter =
			False
		, wraps =
			None Text
		}

let completeToCommand
	: Complete → Command Argument
	=   λ(c : Complete)
	  → let long =
				λ(name : Text)
			  → Prelude.Optional.map
				Text
				Argument
				(   λ(content : Text)
				  → Argument.Option { opt = Option.Long name, arg = content }
				)

		let flag =
				λ(name : Text)
			  → λ(flag : Bool)
			  →       if flag

				then  Some (Argument.Flag (Option.Long name))

				else  None Argument

		let conditionToText =
				λ(c : Command Argument)
			  → Prelude.Text.concatSep " " (argCommandToList c)

		let args =
				[ long "command" (Some c.cmd)
				, long "description" (Some c.description)
				, Prelude.Optional.map
				  (Command Argument)
				  Argument
				  (   λ(c : Command Argument)
					→ Argument.Option
					  { opt = Option.Long "condition", arg = conditionToText c }
				  )
				  c.condition
				, long "short-option" c.short-option
				, long "long-option" c.long-option
				, long "old-option" c.long-option-old-style
				, long "arguments" c.argument
				, long "wraps" c.wraps
				, flag "keep-order" c.keep-order
				, flag "no-files" c.no-files
				, flag "require-parameter" c.require-parameter
				]
			  : List (Optional Argument)

		let id = λ(a : Optional Argument) → a

		in  { cmd =
				"complete"
			, args =
				  List/filterOptional (Optional Argument) Argument id args
				: List Argument
			}

in  let fishSeenSubcommandFn = "__fish_seen_subcommand_from"

	let fishUseSubcommandFn = "__fish_use_subcommand"

	let foo
		: Command Argument
		= completeToCommand
		  (   complete { cmd = "abc", description = "this is foo option" }
			⫽ { condition =
				  Some { cmd = fishUseSubcommandFn, args = [] : List Argument }
			  , argument =
				  Some "foo"
			  }
		  )

	in    [ argCommandToList foo, [ "complete", "--do-complete=abc " ] ]
		: List (List Text)