const WebpackCLI = require('./webpack-cli');
const { core, commands } = require('./utils/cli-flags');
const cmdArgs = require('command-line-args');

require('./utils/process-log');

const isFlagPresent = (args, flag) => args.find(arg => [flag, `--${flag}`].includes(arg));
const isArgCommandName = (arg, cmd) => arg === cmd.name || arg === cmd.alias;
const stripDashedFlags = (args, cmd) => {
    const slicedArgs = args.slice(2);
    if (cmd.name === 'serve') {
        return slicedArgs.filter(arg => !isArgCommandName(arg, cmd));
    }
    else {
        return slicedArgs.filter(arg => ~arg.indexOf('--') && !isArgCommandName(arg, cmd));
    }
};

const isCommandUsed = commands =>
    commands.find(cmd => {
        return process.argv.includes(cmd.name) || process.argv.includes(cmd.alias);
    });

async function runCLI(cli, commandIsUsed) {
    let args;
    const helpFlagExists = isFlagPresent(process.argv, 'help');
    const versionFlagExists = isFlagPresent(process.argv, 'version');

    if (helpFlagExists) {
        cli.runHelp();
        return;
    } else if (versionFlagExists) {
        cli.runVersion();
        return;
    }

    if (commandIsUsed) {
        commandIsUsed.defaultOption = true;
        args = stripDashedFlags(process.argv, commandIsUsed);
        return await cli.runCommand(commandIsUsed, ...args);
    } else {
        try {
            args = cmdArgs(core, { stopAtFirstUnknown: false, partial: true });
            const result = await cli.run(args, core);
            if (!result) {
                return;
            }
        } catch (err) {

            if (err.name === 'UNKNOWN_VALUE') {
                process.cliLogger.error(`Parse Error (unknown argument): ${err.value}`);
                return;
            }
            else if (err.name === 'ALREADY_SET') {
                const argsMap = {};
                const keysToDelete = [];
                process.argv.forEach((arg, idx) => {
                    const oldMapValue = argsMap[arg];
                    argsMap[arg] = {
                        value: process.argv[idx],
                        type: (arg.charAt(0) === '-' && arg.charAt(1) === '-') ? 'dash' : 'cmd',
                        pos: idx
                    }
                    // Swap idx of overriden value
                    if (oldMapValue) {
                        argsMap[arg].pos = oldMapValue.pos;
                        keysToDelete.push(idx + 1);
                    }
                })
                // Filter out the value for the overriden key
                const newArgKeys = Object.keys(argsMap).filter(arg => !keysToDelete.includes(argsMap[arg].pos));
                process.argv = newArgKeys;
                args = cmdArgs(core, { stopAtFirstUnknown: false, partial: true });
                const result = await cli.run(args, core);
                if (!result) {
                    return;
                }
                process.cliLogger.warn(`duplicate flags found, defaulting to last set value`);
            } else {
                process.cliLogger.error(err);
            }
        }
    }
}

const commandIsUsed = isCommandUsed(commands);
const cli = new WebpackCLI();
runCLI(cli, commandIsUsed);

@@ -379,27 +379,136 @@
 rgs, cmd) =%3E
+ args.filter(arg =%3E ~arg.indexOf('--') && !isArgCommandName(arg, cmd));%0Aconst normalizeFlags = (args, cmd) =%3E
  %7B%0A
-
     const sl
@@ -640,20 +640,16 @@
 );%0A    %7D
-%0A   
  else %7B%0A
@@ -668,82 +668,40 @@
 rn s
-licedArgs.filter(arg =%3E ~arg.indexOf('--') && !isArgCommandName(a
+tripDashedFlags(slicedA
 rg
+s
 , cmd)
-)
 ;%0A  
@@ -1289,27 +1289,25 @@
  args = 
-stripDashed
+normalize
 Flags(pr
@@ -1799,32 +1799,32 @@
         return;%0A
+
             %7D%0A  
@@ -1820,28 +1820,16 @@
        %7D
-%0A           
  else if

change helper functions slightly and fix style