From 86c477343d8ae84fc99014a2cc4f844e9c688b9c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 20 Sep 2019 14:40:15 -0700 Subject: [PATCH] Updates to error handling in the command line parsing and usage printing. --- nomsu.lua | 27 +++++++++++++++++++++------ nomsu.moon | 24 ++++++++++++++++++------ 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/nomsu.lua b/nomsu.lua index f35c4a1..872b49f 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -1,7 +1,7 @@ NOMSU_VERSION = { 7, 0, - 0 + 1 } local clibtype = package.cpath:match("?%.(so)") or package.cpath:match("?%.(dll)") if NOMSU_PREFIX then @@ -29,9 +29,9 @@ end NOMSU_VERSION = List(NOMSU_VERSION) local Text = require('text') require('builtin_metatables') -local usage = [=[Nomsu Compiler - -Usage: (nomsu | lua nomsu.lua | moon nomsu.moon) [-V version] [--help | -h] [--version] [-O optimization level] [-v] [-c] [-s] [-d debugger] [--no-core] [(file | -t tool | -e "nomsu code..." | files... -- ) [nomsu args...]] +local EXIT_FAILURE = 1 +local EXIT_SUCCESS = 0 +local usage = [=[Nomsu Usage: nomsu [-V version] [--help | -h] [--version] [-O optimization level] [-v] [-c] [-s] [-d debugger] [--no-core] [(file | -t tool | -e "nomsu code..." | files... -- ) [nomsu args...]] OPTIONS -t Run a tool. @@ -101,13 +101,19 @@ local parser = re.compile([[ args <- {| (flag %sep)* }) local arg_string = table.concat(arg, sep) .. sep local args, err = parser:match(arg_string) -if not args or err or args.help then +if not args or err then if err then print("Didn't understand: " .. tostring(err)) end print(usage) os.exit(EXIT_FAILURE) end +if args.help then + print("Nomsu - A dynamically typed programming language with natural syntax and strong metaprogramming abilities.") + print("https://nomsu.org\n") + print(usage) + os.exit(EXIT_SUCCESS) +end if args.version then print(NOMSU_VERSION:joined_with(".")) os.exit(EXIT_SUCCESS) @@ -165,7 +171,7 @@ run = function() nomsu_environment:export("core") end local input_files = { } - if args.files then + if args.files and #args.files > 0 then local _list_1 = args.files for _index_0 = 1, #_list_1 do local f = _list_1[_index_0] @@ -258,6 +264,15 @@ run = function() end else local f = Files.read(filename) + if not f then + if filename:match("^-") then + print("Not a valid flag: " .. filename .. "\n") + print(usage) + else + print("File not found: " .. filename) + end + os.exit(EXIT_FAILURE) + end if filename:match("%.lua$") then f = LuaCode:from(Source(filename, 1, #f), f) end diff --git a/nomsu.moon b/nomsu.moon index b9e36c2..e6058bd 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -1,7 +1,7 @@ #!/usr/bin/env moon -- This file contains the command-line Nomsu runner. export NOMSU_VERSION -NOMSU_VERSION = {7, 0, 0} +NOMSU_VERSION = {7, 0, 1} clibtype = package.cpath\match("?%.(so)") or package.cpath\match("?%.(dll)") @@ -26,11 +26,11 @@ if clibtype == "dll" NOMSU_VERSION = List(NOMSU_VERSION) Text = require 'text' require 'builtin_metatables' +EXIT_FAILURE = 1 +EXIT_SUCCESS = 0 usage = [=[ -Nomsu Compiler - -Usage: (nomsu | lua nomsu.lua | moon nomsu.moon) [-V version] [--help | -h] [--version] [-O optimization level] [-v] [-c] [-s] [-d debugger] [--no-core] [(file | -t tool | -e "nomsu code..." | files... -- ) [nomsu args...]] +Nomsu Usage: nomsu [-V version] [--help | -h] [--version] [-O optimization level] [-v] [-c] [-s] [-d debugger] [--no-core] [(file | -t tool | -e "nomsu code..." | files... -- ) [nomsu args...]] OPTIONS -t Run a tool. @@ -96,11 +96,16 @@ parser = re.compile([[ }) arg_string = table.concat(arg, sep)..sep args, err = parser\match(arg_string) -if not args or err or args.help +if not args or err if err print("Didn't understand: #{err}") print usage os.exit(EXIT_FAILURE) +if args.help + print "Nomsu - A dynamically typed programming language with natural syntax and strong metaprogramming abilities." + print "https://nomsu.org\n" + print usage + os.exit(EXIT_SUCCESS) if args.version print(NOMSU_VERSION\joined_with(".")) os.exit(EXIT_SUCCESS) @@ -139,7 +144,7 @@ run = -> nomsu_environment\export("core") input_files = {} - if args.files + if args.files and #args.files > 0 for f in *args.files if nomsu_name = f\match("^nomsu://(.*)%.nom") path, err = package.searchpath(nomsu_name, package.nomsupath, "/") @@ -204,6 +209,13 @@ run = -> else -- Just run the file f = Files.read(filename) + if not f + if filename\match "^-" + print "Not a valid flag: "..filename.."\n" + print usage + else + print "File not found: "..filename + os.exit EXIT_FAILURE if filename\match("%.lua$") f = LuaCode\from(Source(filename, 1, #f), f) env = nomsu_environment.new_environment!