GithubHelp home page GithubHelp logo

Comments (3)

FaberVitale avatar FaberVitale commented on August 18, 2024

Hi!

Deno.test({
  name: "issue #4",
  fn: () => {
    assertEquals(
      parse(["-T", "ttt"]),
      {
        T: 'ttt',
        _: [],
      },
    );
  },
});

I've justed tested this on deno 1.14.3 and works without any issue.
Could please provide more infos regarding the chrome version?

from deno_minimist.

FaberVitale avatar FaberVitale commented on August 18, 2024

I've run the same test, compiling the lib with deno bundle, in chrome and I cannot reproduce.

Test script
<script type="module">
function hasKey(obj, keys) {
    const lastIndex = keys.length - 1;
    let o = obj;
    for(let i = 0; i < lastIndex; i++){
        o = o[keys[i]] || Object.create(null);
    }
    return keys[lastIndex] in o;
}
function isNumberLike(x) {
    return typeof x === "number" || typeof x === "string" && (/^0x[0-9a-f]+$/i.test(x) || /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x));
}
function computeAliases(opts) {
    const aliases = Object.create(null);
    if (!opts.alias) {
        return aliases;
    }
    const keys = Object.keys(opts.alias);
    for(let i = 0, len = keys.length; i < len; i++){
        const key = keys[i];
        aliases[key] = [].concat(opts.alias[key]);
        for(let j = 0; j < aliases[key].length; j++){
            aliases[aliases[key][j]] = [
                key
            ].concat(aliases[key].filter((y)=>aliases[key][j] !== y
            ));
        }
    }
    return aliases;
}
function createMinimistContext(inputArgs, opts) {
    const options = opts || Object.create(null);
    const defaults = Object.assign(Object.create(null), options.default);
    const flags = {
        bools: Object.create(null),
        strings: Object.create(null),
        allBools: false
    };
    const aliases = computeAliases(options);
    const output = {
        args: inputArgs,
        options,
        defaults,
        flags,
        aliases,
        unknownFn: null,
        argv: Object.assign(Object.create(null), {
            _: []
        }),
        notFlags: []
    };
    if (typeof options.unknown === "function") {
        output.unknownFn = options["unknown"];
    }
    if (options.boolean) {
        if (typeof options.boolean === "boolean") {
            flags.allBools = true;
        } else if (typeof options.boolean === "string") {
            flags.bools[options.boolean] = true;
        } else if (Array.isArray(options.boolean)) {
            for(let i = 0, len = options.boolean.length; i < len; i++){
                flags.bools[options.boolean[i]] = true;
            }
        }
    }
    if (options.string) {
        const strings = [].concat(options.string);
        for(let i = 0, len = options.string.length; i < len; i++){
            const key = strings[i];
            if (key) {
                flags.strings[key] = true;
            }
            if (aliases[key]) {
                flags.strings[aliases[key] + ""] = true;
            }
        }
    }
    if (output.args.indexOf("--") !== -1) {
        output.notFlags = inputArgs.slice(inputArgs.indexOf("--") + 1);
        output.args = inputArgs.slice(0, inputArgs.indexOf("--"));
    }
    return output;
}
function aliasIsBoolean({ aliases , flags  }, key) {
    return aliases[key].some((val)=>flags.bools[val]
    );
}
function argDefined({ aliases , flags  }, key, arg) {
    return flags.allBools && /^--[^=]+$/.test(arg) || flags.strings[key] || flags.bools[key] || aliases[key];
}
function setKey({ flags  }, obj, keys, value) {
    let o = obj;
    const len = keys.length;
    for(let i = 0; i < len - 1; i++){
        const key = keys[i];
        if (key === "__proto__") {
            return;
        }
        if (o[key] === undefined) {
            o[key] = Object.create(null);
        }
        o = o[key];
    }
    const key = keys[len - 1];
    if (key === "__proto__") {
        return;
    }
    if (o === Object.prototype || o === Number.prototype || o === String.prototype) {
        o = Object.create(null);
    }
    if (o === Array.prototype) {
        o = [];
    }
    if (o[key] === undefined || flags.bools[key] || typeof o[key] === "boolean") {
        o[key] = value;
    } else if (Array.isArray(o[key])) {
        o[key].push(value);
    } else {
        o[key] = [
            o[key],
            value
        ];
    }
}
function setArg(ctx, key, val, arg) {
    if (arg && ctx.unknownFn && !argDefined(ctx, key, arg)) {
        if (ctx.unknownFn(arg) === false) {
            return;
        }
    }
    const value = !ctx.flags.strings[key] && isNumberLike(val) ? Number(val) : val;
    setKey(ctx, ctx.argv, key.split("."), value);
    if (ctx.aliases[key]) {
        for(let i = 0, len = ctx.aliases[key].length; i < len; i++){
            setKey(ctx, ctx.argv, ctx.aliases[key][i].split("."), value);
        }
    }
}
function consumeArgs(ctx) {
    const { flags , aliases , args , argv , options  } = ctx;
    const boolsKeys = Object.keys(ctx.flags.bools);
    for(let i = 0, len = boolsKeys.length; i < len; i++){
        const key = boolsKeys[i];
        setArg(ctx, key, ctx.defaults[key] === undefined ? false : ctx.defaults[key]);
    }
    for(let i1 = 0, len1 = args.length; i1 < len1; i1++){
        const arg = args[i1];
        if (/^--.+=/.test(arg)) {
            const match = arg.match(/^--([^=]+)=([\s\S]*)$/);
            const key = match[1];
            let value = match[2];
            if (flags.bools[key]) {
                value = value !== "false";
            }
            setArg(ctx, key, value, arg);
        } else if (/^--no-.+/.test(arg)) {
            const key = arg.match(/^--no-(.+)/)[1];
            setArg(ctx, key, false, arg);
        } else if (/^--.+/.test(arg)) {
            const key = arg.match(/^--(.+)/)[1];
            const next = args[i1 + 1];
            if (next !== undefined && !/^-/.test(next) && !flags.bools[key] && !flags.allBools && (aliases[key] ? !aliasIsBoolean(ctx, key) : true)) {
                setArg(ctx, key, next, arg);
                i1++;
            } else if (/^(true|false)$/.test(next)) {
                setArg(ctx, key, next === "true", arg);
                i1++;
            } else {
                setArg(ctx, key, flags.strings[key] ? "" : true, arg);
            }
        } else if (/^-[^-]+/.test(arg)) {
            const letters = arg.slice(1, -1).split("");
            let broken = false;
            for(let j = 0; j < letters.length; j++){
                const next = arg.slice(j + 2);
                if (next === "-") {
                    setArg(ctx, letters[j], next, arg);
                    continue;
                }
                if (/[A-Za-z]/.test(letters[j]) && /=/.test(next)) {
                    setArg(ctx, letters[j], next.split("=")[1], arg);
                    broken = true;
                    break;
                }
                if (/[A-Za-z]/.test(letters[j]) && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
                    setArg(ctx, letters[j], next, arg);
                    broken = true;
                    break;
                }
                if (letters[j + 1] && letters[j + 1].match(/\W/)) {
                    setArg(ctx, letters[j], arg.slice(j + 2), arg);
                    broken = true;
                    break;
                } else {
                    setArg(ctx, letters[j], flags.strings[letters[j]] ? "" : true, arg);
                }
            }
            const key = arg.slice(-1)[0];
            if (!broken && key !== "-") {
                if (args[i1 + 1] && !/^(-|--)[^-]/.test(args[i1 + 1]) && !flags.bools[key] && (aliases[key] ? !aliasIsBoolean(ctx, key) : true)) {
                    setArg(ctx, key, args[i1 + 1], arg);
                    i1++;
                } else if (args[i1 + 1] && /^(true|false)$/.test(args[i1 + 1])) {
                    setArg(ctx, key, args[i1 + 1] === "true", arg);
                    i1++;
                } else {
                    setArg(ctx, key, flags.strings[key] ? "" : true, arg);
                }
            }
        } else {
            if (!ctx.unknownFn || ctx.unknownFn(arg) !== false) {
                const nonOptionArg = flags.strings["_"] || !isNumberLike(arg) ? arg : Number(arg);
                argv._.push(nonOptionArg);
            }
            if (options.stopEarly) {
                argv._.push.apply(argv._, args.slice(i1 + 1));
                break;
            }
        }
    }
    Object.keys(ctx.defaults).forEach(function(key) {
        if (!hasKey(argv, key.split("."))) {
            setKey(ctx, argv, key.split("."), ctx.defaults[key]);
            (aliases[key] || []).forEach(function(x) {
                setKey(ctx, argv, x.split("."), ctx.defaults[key]);
            });
        }
    });
    if (ctx.options["--"]) {
        argv["--"] = ctx.notFlags;
    } else {
        argv._.push.apply(argv._, ctx.notFlags);
    }
    return argv;
}
const parse = function parse(args, opts) {
    if (!args) {
        throw new TypeError("minimist: no args provided");
    }
    const ctx = createMinimistContext(args, opts);
    return consumeArgs(ctx);
};

console.log(parse(['-T', 'ttt']));
</script> 

output

{
T: 'ttt',
 _: [],
}

from deno_minimist.

mathiasrw avatar mathiasrw commented on August 18, 2024

I must have done something wrong. Im so sorry I stole your time to replicate this. I know how annoying it is with people coming with things that are a local issue (from maintaining alasql). Ill close it and come back if the problem persists.

from deno_minimist.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.