Skip to content

Argument parsing#68

Draft
eritbh wants to merge 5 commits intomainfrom
arg-parsing
Draft

Argument parsing#68
eritbh wants to merge 5 commits intomainfrom
arg-parsing

Conversation

@eritbh
Copy link
Owner

@eritbh eritbh commented Apr 30, 2021

Fixes #25 (or at least it will).

This should be ready for at least basic testing, though it doesn't come with much at the moment. The available argument types out of the box are integer, number, and member. Options for each argument type are documented in the source, but aren't exposed super well in intellisense without Typescript's generics being attached to the method. Not sure if there's much I can do about that, but I'll make sure the built-in ones are outlined well on the site and stuff at least.

To use with JS:

const [member, num] = await parseArgs(args, [
  {
    type: 'member',
    me: msg.member,
    guild: msg.channel.guild,
  },
  {
    type: 'integer',
    lowerBound: 1,
  },
]);

To register a custom argument type:

registerArgumentType('foo', (args, options) => {
  // do something with args to get your type
  // options is one of the objects passed to parseArgs, you can pull custom options from there
  // remember to modify the args array so other argument resolvers don't try to parse the same text
  return args.shift().length > options.customThing;
// (async resolvers are also supported)
});
const [val] = await parseArgs(['somethingVeryLong'], [
  {
    type: 'foo',
    customThing: 3,
  }
]);
val //> true (since "somethingVeryLong" has more than 3 characters)

@eritbh eritbh added enhancement something doesn't happen that should changes: api modifies behavior of the public API version: minor semver-minor, involves feature addition labels Apr 30, 2021
@eritbh eritbh marked this pull request as draft April 30, 2021 05:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes: api modifies behavior of the public API enhancement something doesn't happen that should version: minor semver-minor, involves feature addition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optional command argument parsing

1 participant