all repos — py-vite @ 915a86e13b2259f60a1632fbf9e9d1ec8f40ef20

the original vite, written in python

fix(cli.py): sane argument parsing
Anirudh icyph0x@pm.me
Tue, 24 Apr 2018 21:34:13 +0530
commit

915a86e13b2259f60a1632fbf9e9d1ec8f40ef20

parent

f7bb31f700bbf93fcd08dee33bf5103a3bae9a5c

2 files changed, 25 insertions(+), 24 deletions(-)

jump to
M vite/cli.pyvite/cli.py

@@ -1,6 +1,6 @@

import argparse import os - +import sys from vite import vite from huepy import *

@@ -8,23 +8,27 @@

def main(): desc = green('A simple and minimal static site generator.') usage = lightblue('vite') + ' [new | build | serve]' - parser = argparse.ArgumentParser(description=desc, usage=usage) - help_text = 'Commands to create, build and serve your project.' - parser.add_argument('action', choices=['new', 'build', 'serve'], help=help_text) - parser.add_argument('path', nargs='*') + # parser = argparse.ArgumentParser(description=desc, usage=usage) + parser = argparse.ArgumentParser() + # help_text = 'Commands to create, build and serve your project.' + sp = parser.add_subparsers(dest='cmd') + spp = sp.add_parser('new') + for cmd in ['build', 'serve']: + sp.add_parser(cmd) + spp.add_argument('path') + args = parser.parse_args() - project_path = args.path[0] if len(sys.argv) == 1: parser.print_help() sys.exit(1) - elif args.new: - vite.create_project(project_path) - elif args.build: + elif args.cmd == 'new': + if args.path: + vite.create_project(args.path) + else: + parser.print_help() + elif args.cmd == 'build': vite.builder() - elif args.serve: + elif args.cmd == 'serve': vite.server() else: parser.print_help() - -if args.new: - vite.create_project(project_path)
M vite/vite.pyvite/vite.py

@@ -17,12 +17,19 @@ from huepy import *

from distutils.dir_util import copy_tree from vite import vite +try: + sys.path.append(os.getcwd()) + import config +except FileNotFoundError: + print(bad('Error: config.py not found')) + # constants PAGES_PATH = 'pages/' BUILD_PATH = 'build/' TEMPL_PATH = 'templates/' TEMPL_FILE = TEMPL_PATH + config.template PORT = 1911 + def create_project(path): try:

@@ -76,16 +83,6 @@ <footer>

""") -# argument parsing -#def parser(): -# desc = green('A simple and minimal static site generator.') -# usage = lightblue('vite') + ' [new | build | serve]' -# parser = argparse.ArgumentParser(description=desc, usage=usage) -# parser.add_argument('action', choices=['new', 'build', 'serve'], help='Commands to create, build and serve your project.') -# parser.add_argument('path', nargs='*') -# return parser - - # jinja2 def jinja_render(html_text, TEMPL_FILE): template_loader = jinja2.FileSystemLoader('./')

@@ -118,7 +115,7 @@

def server(): handler = http.server.SimpleHTTPRequestHandler - os.chdir(os.path.join(os.getcwd(), BUILD_PATH) + os.chdir(os.path.join(os.getcwd(), BUILD_PATH)) try: with socketserver.TCPServer(('', PORT), handler) as httpd: print(run(f'Serving the {italic("build")} directory at http://localhost:{PORT}'))