Skip to content

Commit 5e84217

Browse files
cli: Replace docopt with urfave/cli. (#48)
* cli: Replace docopt with urfave/cli. Fixes issue 25. * docs: Add subheadings to Readme. * nit: Rearrange imports. * nit: Compress if.
1 parent 55c9bab commit 5e84217

File tree

9 files changed

+274
-150
lines changed

9 files changed

+274
-150
lines changed

Readme.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

Readme.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# SCIP Code Intelligence Protocol
2+
3+
SCIP (pronounciation: "skip") is a language-agnostic protocol
4+
for indexing source code,
5+
which can be used to power code navigation functionality
6+
such as Go to definition, Find references, and Find implementations.
7+
8+
This repository includes:
9+
10+
- A [protobuf schema for SCIP](./scip.proto).
11+
- Rich Go bindings for SCIP: This includes many utility functions
12+
to help build tooling on top of SCIP.
13+
- Auto-generated bindings for TypeScript, Rust and Haskell.
14+
- The [`scip` CLI](#scip-cli-reference) makes SCIP indexes
15+
a breeze to work with.
16+
17+
If you're interested in better understanding the motivation behind SCIP,
18+
check out the [announcement blog post](https://about.sourcegraph.com/blog/announcing-scip).
19+
20+
If you're interested in writing a new indexer that emits SCIP,
21+
check out our documentation on
22+
[how to write an indexer](https://docs.sourcegraph.com/code_intelligence/explanations/writing_an_indexer).
23+
24+
## Contributing
25+
26+
We welcome questions, suggestions as well as code and docs contributions.
27+
28+
For submitting contributions, check out [Development.md](./Development.md)
29+
to better understand project structure and common workflows.
30+
31+
Contributors should abide by the [Sourcegraph Code of Conduct](https://handbook.sourcegraph.com/company-info-and-process/communication/code_of_conduct/).
32+
33+
## SCIP CLI reference
34+
35+
```
36+
NAME:
37+
scip - SCIP Code Intelligence Protocol CLI
38+
39+
USAGE:
40+
scip [global options] command [command options] [arguments...]
41+
42+
DESCRIPTION:
43+
For more details, see the project README at:
44+
45+
https://github.com/sourcegraph/scip
46+
47+
COMMANDS:
48+
convert Convert a SCIP index to an LSIF index
49+
snapshot Generate snapshot files for golden testing
50+
stats Output useful statistics about a SCIP index
51+
help, h Shows a list of commands or help for one command
52+
53+
GLOBAL OPTIONS:
54+
--help, -h show help (default: false)
55+
--version, -v Print the current version and exit. (default: false)
56+
```
57+
58+
### `scip convert`
59+
60+
```
61+
NAME:
62+
scip convert - Convert a SCIP index to an LSIF index
63+
64+
USAGE:
65+
scip convert [command options] [arguments...]
66+
67+
OPTIONS:
68+
--from value Path to SCIP index file (default: index.scip)
69+
--to value Output path for LSIF index (default: dump.lsif)
70+
```
71+
72+
### `scip snapshot`
73+
74+
```
75+
NAME:
76+
scip snapshot - Generate snapshot files for golden testing
77+
78+
USAGE:
79+
scip snapshot [command options] [arguments...]
80+
81+
DESCRIPTION:
82+
The snapshot subcommand generates snapshot files which
83+
can be use for inspecting the output of an index in a
84+
visual way. Occurrences are marked with caret signs (^)
85+
and symbol information.
86+
87+
OPTIONS:
88+
--from value Path to SCIP index file (default: index.scip)
89+
--to value Path to output directory for snapshot files (default: scip-snapshot)
90+
```
91+
92+
### `scip stats`
93+
94+
```
95+
NAME:
96+
scip stats - Output useful statistics about a SCIP index
97+
98+
USAGE:
99+
scip stats [command options] [arguments...]
100+
101+
OPTIONS:
102+
--from value Path to SCIP index file (default: index.scip)
103+
```

cmd/Readme.md

Lines changed: 0 additions & 66 deletions
This file was deleted.

cmd/convert.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,48 @@ import (
55
"os"
66
"strings"
77

8+
"github.com/urfave/cli/v2"
9+
810
"github.com/sourcegraph/sourcegraph/lib/codeintel/lsif/protocol/reader"
911
"github.com/sourcegraph/sourcegraph/lib/errors"
1012

1113
"github.com/sourcegraph/scip/bindings/go/scip"
1214
)
1315

14-
func convertMain(parsedArgs map[string]interface{}) error {
15-
scipIndex, err := readFromOption(parsedArgs["--from"].(string))
16+
type convertFlags struct {
17+
from string
18+
to string
19+
}
20+
21+
func convertCommand() cli.Command {
22+
var convertFlags convertFlags
23+
convert := cli.Command{
24+
Name: "convert",
25+
Usage: "Convert a SCIP index to an LSIF index",
26+
Flags: []cli.Flag{
27+
fromFlag(&convertFlags.from),
28+
&cli.StringFlag{
29+
Name: "to",
30+
Usage: "Output path for LSIF index",
31+
Destination: &convertFlags.to,
32+
DefaultText: "dump.lsif",
33+
},
34+
},
35+
Action: func(c *cli.Context) error {
36+
return convertMain(convertFlags)
37+
},
38+
}
39+
return convert
40+
}
41+
42+
func convertMain(flags convertFlags) error {
43+
scipIndex, err := readFromOption(flags.from)
1644
if err != nil {
1745
return err
1846
}
1947

2048
var lsifWriter io.Writer
21-
toPath := parsedArgs["--to"].(string)
49+
toPath := flags.to
2250
if toPath == "-" {
2351
lsifWriter = os.Stdout
2452
} else if !strings.HasSuffix(toPath, ".lsif") {

cmd/main.go

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
package main
22

33
import (
4-
_ "embed"
54
"fmt"
5+
"log"
66
"os"
7-
"regexp"
87

9-
"github.com/docopt/docopt-go"
8+
"github.com/urfave/cli/v2"
109
)
1110

12-
//go:embed Readme.md
13-
var readme string
14-
15-
func helpText() string {
16-
usageRegexp := regexp.MustCompile("(?s)<!-- begin usage -->\n\n```\n(.*)\n```\n\n<!-- end usage -->")
17-
usageText := usageRegexp.FindStringSubmatch(readme)[1]
18-
return fmt.Sprintf(helpTextTemplate, usageText)
11+
func main() {
12+
app := scipApp()
13+
if err := app.Run(os.Args); err != nil {
14+
log.Fatal(err)
15+
}
1916
}
2017

21-
const helpTextTemplate string = `Semantic Code Intelligence Protocol CLI.
22-
23-
%s
24-
25-
For more details, see the project README: https://github.com/sourcegraph/scip
26-
`
18+
func commands() []*cli.Command {
19+
convert := convertCommand()
20+
snapshot := snapshotCommand()
21+
stats := statsCommand()
22+
return []*cli.Command{&convert, &snapshot, &stats}
23+
}
2724

28-
func bailIfError(err error) {
29-
if err != nil {
30-
os.Stderr.WriteString(err.Error())
31-
os.Exit(1)
25+
func scipApp() cli.App {
26+
var versionFlag bool
27+
app := cli.App{
28+
Name: "scip",
29+
Usage: "SCIP Code Intelligence Protocol CLI",
30+
Description: "For more details, see the project README at:\n\n\thttps://github.com/sourcegraph/scip",
31+
Flags: []cli.Flag{
32+
&cli.BoolFlag{
33+
Name: "version",
34+
Usage: "Print the current version and exit.",
35+
Destination: &versionFlag,
36+
Aliases: []string{"v"},
37+
},
38+
},
39+
Action: func(c *cli.Context) error {
40+
if versionFlag {
41+
fmt.Println("0.1.0")
42+
os.Exit(0)
43+
}
44+
// FIXME: What is the right way to print help text and error here?
45+
return nil
46+
},
47+
Commands: commands(),
3248
}
49+
return app
3350
}
3451

35-
func main() {
36-
parsedArgs, err := docopt.ParseDoc(helpText())
37-
bailIfError(err)
38-
// --help is handled by docopt
39-
if parsedArgs["--version"].(bool) {
40-
fmt.Println("0.1.0")
41-
os.Exit(0)
42-
}
43-
if parsedArgs["convert"].(bool) {
44-
bailIfError(convertMain(parsedArgs))
45-
os.Exit(0)
46-
}
47-
if parsedArgs["stats"].(bool) {
48-
bailIfError(statsMain(parsedArgs))
49-
os.Exit(0)
50-
}
51-
if parsedArgs["snapshot"].(bool) {
52-
bailIfError(snapshotMain(parsedArgs))
53-
os.Exit(0)
52+
func fromFlag(storage *string) *cli.StringFlag {
53+
return &cli.StringFlag{
54+
Name: "from",
55+
Usage: "Path to SCIP index file",
56+
Destination: storage,
57+
DefaultText: "index.scip",
5458
}
55-
// Normally, this should be impossible as docopt should properly handle
56-
// incorrect arguments, but might as well exit nicely. 🤷🏽
57-
os.Stderr.WriteString(helpText())
58-
os.Exit(1)
5959
}

0 commit comments

Comments
 (0)