Skip to content

Commit dc7419f

Browse files
author
Miqueas Martinez
committed
Finished Nim example
1 parent 92b3a6c commit dc7419f

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

Github.nim

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# TODO: write the example
21
import std/httpclient
2+
import std/strformat
3+
import std/parseopt
34
import std/json
4-
# import std/parseopt
55
from std/os import commandLineParams
66

77
const BASE_URL = "https://api.github.com/users/"
@@ -64,15 +64,64 @@ method fetch*(self: GthUser, thing: string): void {.base.} =
6464
else:
6565
echo "Unsupported endpoint: ", thing
6666

67-
let
68-
args = commandLineParams()
69-
# TODO: handle commandline switches
70-
# opts = initOptParser(args)
67+
var
68+
cmdline = commandLineParams()
69+
args: seq[string]
70+
opts: tuple[
71+
repos: bool,
72+
gists: bool,
73+
followers: bool,
74+
following: bool
75+
]
7176

72-
if args.len() > 0:
77+
for kind, name, value in getopt(cmdline):
78+
case kind:
79+
of cmdArgument: args.add(name)
80+
of cmdShortOption, cmdLongOption:
81+
case name:
82+
of "r", "repos": opts.repos = true
83+
of "g", "gists": opts.gists = true
84+
of "f", "followers": opts.followers = true
85+
of "F", "following": opts.following = true
86+
else: continue
87+
of cmdEnd: break
88+
89+
if args.len() == 0:
90+
echo "No arguments, nothing to do."
91+
else:
7392
for username in args:
7493
var user = newGthUser(username)
75-
echo user.name
76-
echo user.bio
77-
echo user.link
94+
95+
echo "Name: ", user.name
96+
echo "Bio: ", user.bio
97+
echo "Link: ", user.link
98+
99+
echo "Public repos: ", user.repos.count
100+
if opts.repos:
101+
user.fetch("repos")
102+
103+
for i, v in user.repos.arr:
104+
echo fmt"| {(i + 1):03}. {v}"
105+
106+
echo "Public gists: ", user.gists.count
107+
if opts.gists:
108+
user.fetch("gists")
109+
110+
for i, v in user.gists.arr:
111+
echo fmt"| {(i + 1):03}. {v}"
112+
113+
echo "Public followers: ", user.followers.count
114+
if opts.followers:
115+
user.fetch("followers")
116+
117+
for v in user.followers.arr:
118+
echo fmt"| @{v}"
119+
120+
echo "Public following: ", user.following.count
121+
if opts.following:
122+
user.fetch("following")
123+
124+
for v in user.following.arr:
125+
echo fmt"| @{v}"
126+
78127
echo ""

0 commit comments

Comments
 (0)