Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument parsing #68

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Argument parsing #68

wants to merge 5 commits into from

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