--- Module for parsing of commandline arguments
-- @module lqc.cli.arg_parser
-- @alias lib
local argparse = require 'argparse'
local config = require 'lqc.config'

-- Module for easily parsing list of command line arguments

local name_of_executable = 'lqc'
local help_info = 'Property based testing tool written in Lua'
local parser = argparse(name_of_executable, help_info)
parser.error = function(msg)
    error(msg)
end

-- Converts a string to an integer
-- Returns an integer representation of the input string or raises an error on failure.
local function str_to_int(x)
    return tonumber(x)
end

parser:argument('files_or_dirs',
    'List of input files or directories (recursive search) used for testing, default = "."', nil, nil, '*')
parser:mutex(parser:flag('--check', 'Re-checks the set of tests generated by the last seed'), parser:option('--seed -s',
    'Value of the random seed to use, default = seed based on current time', nil, str_to_int))
parser:option('--numtests', 'Number of iterations per property, default = 100', nil, str_to_int)
parser:option('--numshrinks', 'Number of shrinks per failing property, default = 100', nil, str_to_int)
parser:flag('--colors -c', "Enable coloring of test output, default = disabled (doesn't work on Windows!).")
parser:option('--threads -t', "Executes properties in parallel, default = single-threaded (requires Lua Lanes!).", nil,
    str_to_int)

local lib = {}

--- Parses the arguments
-- @return a table containing the config specified by the user;
--         raises an error if parsing failed.
function lib.parse(args)
    local parsed_values = parser:parse(args)
    return config.resolve(parsed_values)
end

return lib

