Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bulletin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Personal Projects
bulletin
Commits
e21eb5b0
Unverified
Commit
e21eb5b0
authored
2 months ago
by
Kevin Lyda
Browse files
Options
Downloads
Patches
Plain Diff
Move commands into a map.
parent
0b9c1fd7
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
dclish/dclish.go
+43
-14
43 additions, 14 deletions
dclish/dclish.go
repl/help.go
+11
-0
11 additions, 0 deletions
repl/help.go
with
54 additions
and
14 deletions
dclish/dclish.go
+
43
−
14
View file @
e21eb5b0
...
...
@@ -11,17 +11,16 @@ type ActionFunc func(*Command) error
// Flag is a flag for a command.
type
Flag
struct
{
Name
string
Value
string
Default
string
Description
string
}
// Flags is the list of flags.
type
Flags
[
]
*
Flag
type
Flags
map
[
string
]
*
Flag
// Command contains the definition of a command, it's flags and subcommands.
type
Command
struct
{
Command
string
Flags
Flags
Args
[]
string
Commands
[]
*
Command
...
...
@@ -30,24 +29,54 @@ type Command struct {
}
// Commands is the full list of commands.
type
Commands
[
]
*
Command
type
Commands
map
[
string
]
*
Command
// ParseAndRun parses a command line and runs the command.
func
(
c
Commands
)
ParseAndRun
(
line
string
)
error
{
// TODO: this doesn't handle a DCL command line completely.
words
:=
strings
.
Fields
(
line
)
fmt
.
Printf
(
"TODO ParseAndRun need to parse flags: %s
\n
"
,
words
)
cmd
:=
strings
.
ToUpper
(
words
[
0
])
for
i
:=
range
c
{
if
c
[
i
]
.
Command
==
cmd
{
if
c
[
i
]
.
Action
==
nil
{
fmt
.
Printf
(
"Command not implemented:
\n
%s
\n
"
,
c
[
i
]
.
Description
)
cmd
,
ok
:=
c
[
strings
.
ToUpper
(
words
[
0
])]
if
!
ok
{
fmt
.
Printf
(
"ERROR: Unknown command '%s'
\n
"
,
words
[
0
])
return
nil
}
err
:=
c
[
i
]
.
Action
(
c
[
i
])
return
err
if
cmd
.
Action
==
nil
{
fmt
.
Printf
(
"ERROR: Command not implemented:
\n
%s
\n
"
,
cmd
.
Description
)
return
nil
}
for
flg
:=
range
cmd
.
Flags
{
cmd
.
Flags
[
flg
]
.
Value
=
cmd
.
Flags
[
flg
]
.
Default
}
fmt
.
Printf
(
"ERROR: Unknown command '%s'
\n
"
,
cmd
)
return
nil
cmd
.
Args
=
[]
string
{}
if
len
(
words
)
==
1
{
return
cmd
.
Action
(
cmd
)
}
for
i
:=
range
words
[
1
:
]
{
if
strings
.
HasPrefix
(
words
[
i
],
"/"
)
{
flag
,
val
,
assigned
:=
strings
.
Cut
(
words
[
i
],
"="
)
if
assigned
{
wordup
:=
strings
.
ToUpper
(
flag
)
flg
,
ok
:=
cmd
.
Flags
[
wordup
]
if
!
ok
{
fmt
.
Printf
(
"ERROR: Flag '%s' not recognised."
,
words
[
i
])
}
flg
.
Value
=
val
}
else
{
// TODO: handle toggle flag.
wordup
:=
strings
.
ToUpper
(
words
[
i
])
value
:=
"true"
if
strings
.
HasPrefix
(
wordup
,
"/NO"
)
{
wordup
=
strings
.
Replace
(
wordup
,
"/NO"
,
"/"
,
1
)
value
=
"false"
}
flg
,
ok
:=
cmd
.
Flags
[
wordup
]
if
!
ok
{
fmt
.
Printf
(
"ERROR: Flag '%s' not recognised."
,
words
[
i
])
}
flg
.
Value
=
value
}
}
}
return
cmd
.
Action
(
cmd
)
}
This diff is collapsed.
Click to expand it.
repl/help.go
0 → 100644
+
11
−
0
View file @
e21eb5b0
// Package repl implements the main event loop.
package
repl
import
(
"git.lyda.ie/kevin/bulletin/dclish"
)
// ActionHelp handles the `HELP` command.
func
ActionHelp
(
_
*
dclish
.
Command
)
error
{
return
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment