-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for preserving indentation with a flag. See #3 for more…
… details - added support for preserving indentation with a flag - use flag package to parse args - replaced Trim with TrimSpace for handling whitespaces better
- Loading branch information
1 parent
c27fe6f
commit e5bcef4
Showing
3 changed files
with
55 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package cli | |
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
) | ||
|
@@ -14,11 +15,11 @@ var ( | |
) | ||
|
||
const ( | ||
Version = "0.0.6" | ||
Version = "0.0.7" | ||
Author = "elliot40404<[email protected]>" | ||
Name = "modo" | ||
Desc = "Modo is a simple cli app that lets you interact any text file that contains checkboxes in the markdown format." | ||
Example = "modo <file>" | ||
Example = "modo <options> <file>" | ||
) | ||
|
||
func help() { | ||
|
@@ -30,38 +31,61 @@ Usage: | |
%s | ||
Options: | ||
-h, --help Show this help message | ||
-v, --version Show version information | ||
-h, --help Show this help message | ||
-v, --version Show version information | ||
-p, --preserve-indent Preserve indentation of the checkboxes | ||
Examples: | ||
%s | ||
`, Name, Version, Desc, Example, Example) | ||
} | ||
|
||
func ParseArgs() (*os.File, error) { | ||
if len(os.Args) < 2 { | ||
type ParsedArgs struct { | ||
PreserveIndent bool | ||
File *os.File | ||
} | ||
|
||
func ParseArgs() (ParsedArgs, error) { | ||
helpFlag := flag.Bool("help", false, "Show help message") | ||
versionFlag := flag.Bool("v", false, "Show version information") | ||
versionLongFlag := flag.Bool("version", false, "Show version information") | ||
indentFlag := flag.Bool("preserve-indent", false, "preserve original identations") | ||
indentLongFlag := flag.Bool("p", false, "preserve original identations") | ||
|
||
flag.Usage = help | ||
|
||
flag.Parse() | ||
|
||
if *helpFlag { | ||
help() | ||
os.Exit(1) | ||
os.Exit(0) | ||
} | ||
arg := os.Args[1] | ||
if arg == "-h" || arg == "--help" { | ||
help() | ||
os.Exit(1) | ||
} | ||
if arg == "-v" || arg == "--version" { | ||
|
||
if *versionFlag || *versionLongFlag { | ||
fmt.Printf("%s %s\n", Name, Version) | ||
os.Exit(0) | ||
} | ||
|
||
if flag.NArg() < 1 { | ||
help() | ||
os.Exit(1) | ||
} | ||
|
||
arg := flag.Arg(0) | ||
|
||
fi, err := os.Stat(arg) | ||
if err != nil { | ||
return nil, ErrFileNotExist | ||
return ParsedArgs{}, ErrFileNotExist | ||
} | ||
if fi.IsDir() { | ||
return nil, ErrNotAFile | ||
return ParsedArgs{}, ErrNotAFile | ||
} | ||
f, err := os.OpenFile(arg, os.O_RDWR, 0o644) | ||
if err != nil { | ||
return nil, ErrFileOpen | ||
return ParsedArgs{}, ErrFileOpen | ||
} | ||
return f, nil | ||
return ParsedArgs{ | ||
PreserveIndent: *indentFlag || *indentLongFlag, | ||
File: f, | ||
}, nil | ||
} |
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