From 6f7aa4c9906a025a916f8a12e5125d60a383a0eb Mon Sep 17 00:00:00 2001 From: Dakota Walsh Date: Mon, 21 Feb 2022 11:48:46 +1300 Subject: [PATCH] refactor options.go Re-arranged the ParseArgs function for improved readability. --- options/options.go | 48 +++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/options/options.go b/options/options.go index a3c017b..5e7b220 100644 --- a/options/options.go +++ b/options/options.go @@ -71,19 +71,25 @@ func ParseArgs(progname string, args []string) (*Opts, string, error) { } var opts Opts opts.Names = flag.Args() + + // Exit early if version is requested. if *versionFlag { return nil, "", ErrVersion } - // Read boolean and string flags. - opts.Output = *outputFlag - opts.TemplateArgs = *templateFlag + // Handle flags. opts.GemOptions = append( opts.GemOptions, gem.WithHorizontalRule(*horizontalRuleFlag), ) - // Create gemtext options from flags. + if *headingNewlineFlag { + opts.GemOptions = append( + opts.GemOptions, + gem.WithHeadingSpace(gem.HeadingSpaceSingle), + ) + } + switch *emphasisFlag { case "none": case "markdown": @@ -101,28 +107,38 @@ func ParseArgs(progname string, args []string) (*Opts, string, error) { ) } - if *headingNewlineFlag { - opts.GemOptions = append(opts.GemOptions, gem.WithHeadingSpace(gem.HeadingSpaceSingle)) - } - switch *headingLinkFlag { case "auto": case "off": - opts.GemOptions = append(opts.GemOptions, gem.WithHeadingLink(gem.HeadingLinkOff)) + opts.GemOptions = append( + opts.GemOptions, + gem.WithHeadingLink(gem.HeadingLinkOff), + ) case "below": - opts.GemOptions = append(opts.GemOptions, gem.WithHeadingLink(gem.HeadingLinkBelow)) + opts.GemOptions = append( + opts.GemOptions, + gem.WithHeadingLink(gem.HeadingLinkBelow), + ) default: - return nil, "", fmt.Errorf("heading link flag type %s is invalid", *headingLinkFlag) + return nil, "", fmt.Errorf("heading link flag type %s is invalid", + *headingLinkFlag) } switch *paragraphLinkFlag { case "below": case "off": - opts.GemOptions = append(opts.GemOptions, gem.WithParagraphLink(gem.ParagraphLinkOff)) + opts.GemOptions = append( + opts.GemOptions, + gem.WithParagraphLink(gem.ParagraphLinkOff), + ) default: - return nil, "", fmt.Errorf("paragraph link flag type %s is invalid", *paragraphLinkFlag) + return nil, "", fmt.Errorf("paragraph link flag type %s is invalid", + *paragraphLinkFlag) } + opts.Output = *outputFlag + opts.TemplateArgs = *templateFlag + if *linkRegexFlag != nil { if len(*linkRegexFlag) > 0 && len(*linkRegexFlag)%3 == 0 { var replacers []gem.LinkReplacer @@ -138,12 +154,14 @@ func ParseArgs(progname string, args []string) (*Opts, string, error) { case "image": r.Type = gem.LinkImage default: - return nil, "", fmt.Errorf("link regex type %s is invalid", (*linkRegexFlag)[i]) + return nil, "", fmt.Errorf("link regex type %s is invalid", + (*linkRegexFlag)[i]) } r.Regex, err = regexp.Compile((*linkRegexFlag)[i+1]) if err != nil { - return nil, "", fmt.Errorf("failed to parse link regex: %v", err) + return nil, "", fmt.Errorf("failed to parse link regex: %v", + err) } r.Replacement = (*linkRegexFlag)[i+2] -- 2.45.2