Skip to content

DRY the CLI#826

Merged
dfellis merged 2 commits into
masterfrom
dry-the-cli
May 3, 2024
Merged

DRY the CLI#826
dfellis merged 2 commits into
masterfrom
dry-the-cli

Conversation

@dfellis

@dfellis dfellis commented Mar 29, 2024

Copy link
Copy Markdown
Collaborator

Based on the discussion with @nrabinowitz in the prior CLI PR. This PR removes duplicate help text and argument types, and turns the arg length trick (and all of the "show subcommand-specific help text" code) into a macro, deleting about twice as much text as it adds.

PS. DRY the CLI does not rhyme.

@dfellis dfellis self-assigned this Mar 29, 2024
@coveralls

coveralls commented Mar 29, 2024

Copy link
Copy Markdown

Coverage Status

coverage: 98.824% (-0.003%) from 98.827%
when pulling ea7489f on dry-the-cli
into b49e72e on master.

@nrabinowitz nrabinowitz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for the cleanup!

Comment thread src/apps/filters/h3.c
if (parseArgs(argc, argv, sizeof(args) / sizeof(Arg *), args, &helpArg, \
args[0]->helpText)) { \
return E_SUCCESS; \
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Do we have a .h header file for this? Should we? If we did, should this macro go there?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We kinda do, but it's shared across many of these binaries while this is only useful here.

On top of that, if I put it in there, I can't make the assumption about the naming of the various variables in use nor that the first argument in the array is declaring the sub-command name with the help text attached, so much of what makes it "magic" here would go away. It would just be a parseArgs function that does the sizeof(args) / sizeof(Arg *) trick for you, and not much else.

Comment thread src/apps/filters/h3.c
Arg cellToBoundaryArg = {
.names = {"cellToBoundary"},
.helpText = "Convert an H3 cell to a WKT POLYGON defining its boundary",
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Total nit: I'd put these next to the functions that implement them, but 🤷

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I waffled back and forth on that. As long as the generalHelp and main functions are the last in the file, that should work just fine, but it might be less obvious why they aren't defined within their function body, so I thought grouping them together as a collection of global variables would at least get people to pause before immediately wanting to refactor it and accidentally break the generalHelp function.

I was also thinking of turning the sub-command function declaration into a macro that makes these global variables at the same time it makes the sub-command function, but I don't know how "magic" we want the syntax in this file to be, so I didn't go that route.

Comment thread src/apps/filters/h3.c
H3Error err = H3_EXPORT(cellToLatLng)(cell, &ll);
if (err) {
return false;
return err;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread src/apps/filters/h3.c Outdated
@dfellis dfellis merged commit 84b511b into master May 3, 2024
@dfellis dfellis deleted the dry-the-cli branch May 3, 2024 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants