Skip to content

Commit

Permalink
feat(nim): add cli folder
Browse files Browse the repository at this point in the history
  • Loading branch information
airscripts committed Jan 6, 2023
1 parent 4649c05 commit 9d5909e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions nim/cli/constants.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from types as types import Function

const CLI_NAME*: string = "Line"
const CLI_VERSION*: string = "1.0.0"
const HELP_FUNCTION: string = "help"
const SHORT_HELP_FUNCTION: string = "h"
const VERSION_FUNCTION: string = "version"
const SHORT_VERSION_FUNCTION: string = "v"
const CLI_COPYRIGHT*: string = "Copyright (c) 2023 by Airscript"

const CLI_FUNCTIONS*: Function = (
h: SHORT_HELP_FUNCTION,
v: SHORT_VERSION_FUNCTION,
help: HELP_FUNCTION,
version: VERSION_FUNCTION,
)
20 changes: 20 additions & 0 deletions nim/cli/descriptions.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from std/strformat import fmt

from constants as constants import CLI_NAME, CLI_VERSION, CLI_COPYRIGHT

let usage: string = """
Usage:
line [options]
"""

let options: string = """
Options:
-h, --help Show this help
-v, --version Show the current version
"""

let help*: string = fmt"""
{CLI_NAME} {CLI_VERSION}
{CLI_COPYRIGHT}{'\n'}
{usage}
{options}"""
12 changes: 12 additions & 0 deletions nim/cli/functions.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from std/strformat import fmt

from descriptions as descriptions import help
from constants as constants import CLI_VERSION

proc help*(): void =
echo descriptions.help
system.quit(errorcode=QuitSuccess)

proc version*(): void =
echo fmt"{CLI_VERSION}{'\n'}"
system.quit(errorcode=QuitSuccess)
24 changes: 24 additions & 0 deletions nim/cli/main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from std/parseopt import getopt, cmdLongOption, cmdShortOption

from functions as functions import help, version

from constants as constants import
CLI_NAME,
CLI_VERSION,
CLI_COPYRIGHT,
CLI_FUNCTIONS

proc options(option: string): void =
case option:
of CLI_FUNCTIONS.h, CLI_FUNCTIONS.help: help()
of CLI_FUNCTIONS.v, CLI_FUNCTIONS.version: version()
else: help()

proc cli(): void =
for kind, key, val in getopt():
case kind
of cmdLongOption, cmdShortOption: options(key)
else: discard

when isMainModule:
cli()
6 changes: 6 additions & 0 deletions nim/cli/types.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type
Function* = tuple
h: string
v: string
help: string
version: string

0 comments on commit 9d5909e

Please sign in to comment.