-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use getopt style short/long argument flags (without using getopt package) #34
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,25 +41,77 @@ type Env struct { | |
} | ||
|
||
func Main(env Env) error { | ||
fs := flag.NewFlagSet("ansisvg", flag.ExitOnError) | ||
var versionFlag = fs.Bool("version", false, "Show version") | ||
fs := flag.NewFlagSet("ansisvg", flag.ContinueOnError) | ||
var versionFlag bool | ||
fs.BoolVar(&versionFlag, "v", false, "") | ||
fs.BoolVar(&versionFlag, "version", false, "Show version") | ||
var fontNameFlag = fs.String("fontname", ansitosvg.DefaultOptions.FontName, "Font name") | ||
var fontFileFlag = fs.String("fontfile", "", "Font file to use and embed") | ||
var fontRefFlag = fs.String("fontref", "", "External font file to reference") | ||
var fontRefFlag = fs.String("fontref", "", "External font URL to use") | ||
var fontSizeFlag = fs.Int("fontsize", ansitosvg.DefaultOptions.FontSize, "Font size") | ||
var terminalWidthFlag = fs.Int("width", 0, "Terminal width (auto)") | ||
var terminalWidthFlag int | ||
fs.IntVar(&terminalWidthFlag, "w", 0, "") | ||
fs.IntVar(&terminalWidthFlag, "width", 0, "Terminal width (auto if not set)") | ||
var colorSchemeFlag = fs.String("colorscheme", ansitosvg.DefaultOptions.ColorScheme, "Color scheme") | ||
var listColorSchemesFlag = fs.Bool("listcolorschemes", false, "List color schemes") | ||
var transparentFlag = fs.Bool("transparent", ansitosvg.DefaultOptions.Transparent, "Transparent background") | ||
var gridModeFlag = fs.Bool("grid", false, "Enable grid mode (sets position for each character)") | ||
var characterBoxSize = boxSize{ | ||
Width: ansitosvg.DefaultOptions.CharacterBoxSize.Width, | ||
Height: ansitosvg.DefaultOptions.CharacterBoxSize.Height, | ||
var gridModeFlag = fs.Bool("grid", false, "Grid mode (sets position for each character)") | ||
var helpFlag bool | ||
fs.BoolVar(&helpFlag, "h", false, "") | ||
fs.BoolVar(&helpFlag, "help", false, "Show help") | ||
var charBoxSize = boxSize{ | ||
Width: ansitosvg.DefaultOptions.CharBoxSize.Width, | ||
Height: ansitosvg.DefaultOptions.CharBoxSize.Height, | ||
} | ||
fs.Var(&charBoxSize, "charboxsize", "Character box size (use pixel units instead of font units)") | ||
// handle error and usage output ourself | ||
fs.Usage = func() {} | ||
fs.SetOutput(io.Discard) | ||
longToShort := map[string]string{ | ||
"help": "h", | ||
"version": "v", | ||
"width": "w", | ||
} | ||
usage := func() { | ||
maxNameLen := 0 | ||
fs.VisitAll(func(f *flag.Flag) { | ||
if len(f.Name) > maxNameLen { | ||
maxNameLen = len(f.Name) | ||
} | ||
}) | ||
|
||
fmt.Fprintf(env.Stdout, ` | ||
%[1]s - Convert ANSI to SVG | ||
Usage: %[1]s [FLAGS] | ||
|
||
Example usage: | ||
program | %[1]s > file.svg | ||
|
||
`[1:], fs.Name()) | ||
fs.VisitAll(func(f *flag.Flag) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should maybe comment that is a ugly hack to skip the short flags |
||
if len(f.Name) == 1 { | ||
return | ||
} | ||
|
||
short := "" | ||
if s, ok := longToShort[f.Name]; ok { | ||
short = ", -" + s | ||
} | ||
|
||
flagNames := f.Name + short | ||
pad := strings.Repeat(" ", maxNameLen-len(flagNames)) | ||
fmt.Fprintf(env.Stdout, "--%s%s%s %s\n", f.Name, short, pad, f.Usage) | ||
}) | ||
} | ||
if err := fs.Parse(env.Args[1:]); err != nil { | ||
return err | ||
} | ||
if helpFlag { | ||
usage() | ||
return nil | ||
} | ||
fs.Var(&characterBoxSize, "charboxsize", "Character box size (forces pixel units instead of font-relative units)") | ||
_ = fs.Parse(env.Args[1:]) | ||
|
||
if *versionFlag { | ||
if versionFlag { | ||
fmt.Fprintln(env.Stdout, env.Version) | ||
return nil | ||
} | ||
|
@@ -96,10 +148,10 @@ func Main(env Env) error { | |
FontEmbedded: fontEmbedded, | ||
FontRef: *fontRefFlag, | ||
FontSize: *fontSizeFlag, | ||
TerminalWidth: *terminalWidthFlag, | ||
CharacterBoxSize: svgscreen.BoxSize{ | ||
Width: characterBoxSize.Width, | ||
Height: characterBoxSize.Height, | ||
TerminalWidth: terminalWidthFlag, | ||
CharBoxSize: svgscreen.BoxSize{ | ||
Width: charBoxSize.Width, | ||
Height: charBoxSize.Height, | ||
}, | ||
ColorScheme: *colorSchemeFlag, | ||
Transparent: *transparentFlag, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-fontsize 40 -fontname Arial -charboxsize 50x50 -grid | ||
--fontsize 40 --fontname Arial --charboxsize 50x50 --grid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-colorscheme Slate | ||
--colorscheme Slate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-transparent | ||
--transparent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-fontfile UbuntuMonoNerdFontMono-Regular.woff2 | ||
--fontfile UbuntuMonoNerdFontMono-Regular.woff2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-fontname Monaco | ||
--fontname Monaco |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-fontref font.woff2 | ||
--fontref font.woff2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-fontfile UbuntuMonoNerdFontMono-Regular.woff2 -grid | ||
--fontfile UbuntuMonoNerdFontMono-Regular.woff2 --grid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-width 4 | ||
--width 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
-colorscheme "Builtin Solarized Light" | ||
--colorscheme "Builtin Solarized Light" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shorted some texts to stay within 80 characters. Otherwise help text look good? should include something more?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM