Skip to content

Commit

Permalink
add help/version command
Browse files Browse the repository at this point in the history
  • Loading branch information
konoui committed May 6, 2024
1 parent 0dbe213 commit dcc096b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ BINARY := bin/$(BIN_NAME)
GOLANGCI_LINT_VERSION := v1.52.2
export GO111MODULE=on

CMD_PACKAGE_DIR := github.com/konoui/lipo/cmd
LDFLAGS := -X '$(CMD_PACKAGE_DIR).Version=$(VERSION)' -X '$(CMD_PACKAGE_DIR).Revision=$(REVISION)'

## Build binaries on your environment
build:
CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o $(BINARY) $(SRC_DIR)
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ hello-world (for architecture arm64): Mach-O 64-bit executable arm64
### Supported Options

`-archs`, `-create`, `-extract`, `-extract_family`, `-output`, `-remove`, `-replace`, `-segalign`, `-thin`, `-verify_arch`, `-arch`, `-info`, `-detailed_info`, `-hideARM64`, `-fat64`

Please run the `-help` command for more details.

```
$ lipo -help
```
24 changes: 22 additions & 2 deletions cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ func fatal(w io.Writer, msg string) (exitCode int) {
return 1
}

var (
Version = "*"
Revision = "*"
)

func Execute(stdout, stderr io.Writer, args []string) (exitCode int) {
fset := sflag.NewFlagSet("lipo")
helpGroup := fset.NewGroup("help")
versionGroup := fset.NewGroup("version")
createGroup := fset.NewGroup("create").AddDescription(createDescription)
thinGroup := fset.NewGroup("thin").AddDescription(thinDescription)
extractGroup := fset.NewGroup("extract").AddDescription(extractDescription)
Expand All @@ -26,12 +33,17 @@ func Execute(stdout, stderr io.Writer, args []string) (exitCode int) {
verifyArchGroup := fset.NewGroup("verify_arch").AddDescription(verifyArchDescription)
infoGroup := fset.NewGroup("info").AddDescription(infoDescription)
detailedInfoGroup := fset.NewGroup("detailed_info").AddDescription(detailedInfoDescription)
groups := []*sflag.Group{createGroup, thinGroup, extractGroup,
groups := []*sflag.Group{
helpGroup, versionGroup,
createGroup, thinGroup, extractGroup,
extractFamilyGroup, removeGroup, replaceGroup,
archsGroup, verifyArchGroup, infoGroup,
detailedInfoGroup,
}
fset.Usage = sflag.UsageFunc(groups...)
// original lipo does not have help/version command.
help := fset.Bool("help", "-help")
version := fset.Bool("version", "-version")
out := fset.String("output", "-output <output_file>", sflag.WithShortName("o"))
segAligns := fset.FixedStringFlags("segalign", "-segalign <arch_type> <alignment>", sflag.WithShortName("s"))
arch := fset.FixedStringFlags("arch", "-arch <arch_type> <input_file>", sflag.WithShortName("a"))
Expand All @@ -48,6 +60,8 @@ func Execute(stdout, stderr io.Writer, args []string) (exitCode int) {
hideArm64 := fset.Bool("hideARM64", "-hideARM64")
fat64 := fset.Bool("fat64", "-fat64")

helpGroup.AddRequired(help)
versionGroup.AddRequired(version)
createGroup.
AddRequired(create).
AddRequired(out).
Expand Down Expand Up @@ -188,8 +202,14 @@ func Execute(stdout, stderr io.Writer, args []string) (exitCode int) {
return 1
}
return
case "version":
fmt.Fprintf(stdout, "%s/%s\n", Version, Revision)
return 0
case "help":
fmt.Fprint(stdout, fset.Usage())
return 0
default:
fset.Usage()
fmt.Fprint(stderr, fset.Usage())
return 1
}
}
Expand Down

0 comments on commit dcc096b

Please sign in to comment.