-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.nim
77 lines (54 loc) · 1.82 KB
/
args.nim
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
import tables
import strformat
from os import commandLineParams, existsEnv
type
Action* = enum
activate, deactivate, create, remove, completions
Arguments = ref object
case action*: Action
of activate, create, remove: name * : string
of completions: previous*, current*: string
of deactivate: discard
#
# module private types
#
type
ActionFormat = tuple[kind: Action, numArgs: int]
const actionDescriptions* = toTable[string, ActionFormat](
{
"activate": (Action.activate, 1),
"create": (Action.create, 1),
"remove": (Action.remove, 1),
"deactivate": (Action.deactivate, 0),
})
proc usage() =
echo("""usage: cnda <comand> [<argument>]
The supported commands are:
activate <argument> activate conda environment
deactivate deactivate conda environment
create <argument> create new conda environment
remove <argument> remove conda environment
The <argument> argument specifies the conda environment for the
command. It can be environment's name or environment.yml file.""")
quit(1)
proc parseArgs*(): Arguments =
let args = commandLineParams()
if existsEnv("COMP_LINE"):
# the 'bash completions' mode detected
return Arguments(action: completions, previous: args[2], current: args[1])
if args.len < 1:
usage()
let actionArg = args[0]
if actionArg == "--help" or actionArg == "-h":
usage()
if not actionDescriptions.hasKey(actionArg):
echo &"unknown action '{actionArg}'"
quit(1)
let actionDesc = actionDescriptions[actionArg]
result = Arguments(action: actionDesc.kind)
if actionDesc.numArgs == 0:
return
if args.len < 2:
echo &"'{actionArg}' requires an argument"
quit(1)
result.name = args[1]