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

Generate optstrings automatically #864

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/bin/pgcopydb/cli_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,34 @@ cli_read_one_line(const char *filename,
}


/*
* construct_optstring constructs the optstring for getopt_long based on the
* long_options array.
*/
char *
construct_optstring(struct option *long_options)
{
PQExpBuffer buf = createPQExpBuffer();

while (long_options->name != NULL)
{
appendPQExpBufferChar(buf, long_options->val);

if (long_options->has_arg)
{
appendPQExpBufferChar(buf, ':');
}

long_options++;
}

char *optstring = strdup(buf->data);
destroyPQExpBuffer(buf);

return optstring;
}


/*
* cli_copy_db_getopts parses the CLI options for the `copy db` command.
*/
Expand Down Expand Up @@ -650,8 +678,7 @@ cli_copy_db_getopts(int argc, char **argv)
exit(EXIT_CODE_BAD_ARGS);
}

const char *optstring =
"S:T:D:J:I:b:L:u:mcAPOXj:xBeMlUagkyF:F:Q:irRCN:fp:ws:o:tE:Vvdzqh";
const char *optstring = construct_optstring(long_options);

while ((c = getopt_long(argc, argv,
optstring, long_options, &option_index)) != -1)
Expand Down
2 changes: 2 additions & 0 deletions src/bin/pgcopydb/cli_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,6 @@ bool cli_parse_bytes_pretty(const char *byteString,

bool cli_prepare_pguris(ConnStrings *connStrings);

char * construct_optstring(struct option *long_options);

#endif /* CLI_COMMON_H */
4 changes: 3 additions & 1 deletion src/bin/pgcopydb/cli_compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ cli_compare_getopts(int argc, char **argv)
SplitTableLargerThan empty = { 0 };
options.splitTablesLargerThan = empty;

while ((c = getopt_long(argc, argv, "S:T:D:j:JVvdzqh",
const char *optstring = construct_optstring(long_options);

while ((c = getopt_long(argc, argv, optstring,
long_options, &option_index)) != -1)
{
switch (c)
Expand Down
4 changes: 3 additions & 1 deletion src/bin/pgcopydb/cli_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ cli_dump_schema_getopts(int argc, char **argv)
exit(EXIT_CODE_BAD_ARGS);
}

while ((c = getopt_long(argc, argv, "S:T:D:PrReFCNVvdzqh",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should have N: as snapshot has a required argument.

{ "snapshot", required_argument, NULL, 'N' }

I wonder if this means we never supported pgcopydb dump schema --snapshot <snapshot_id> properly.

Copy link
Owner

Choose a reason for hiding this comment

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

I see. Can we include a fix for the argument parsing in this PR then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Once we programmatically create the optstring values, it will fix this underlying issue as well.

This bug only exists in the line that I want to remove.

const char *optstring = construct_optstring(long_options);

while ((c = getopt_long(argc, argv, optstring,
long_options, &option_index)) != -1)
{
switch (c)
Expand Down
2 changes: 1 addition & 1 deletion src/bin/pgcopydb/cli_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ cli_list_db_getopts(int argc, char **argv)
exit(EXIT_CODE_BAD_ARGS);
}

const char *optstring = "S:D:s:t:F:xPL:u:k:mfyarJRIN:Vdzvqh";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should not have k: as it has no arg. { "skip-split-by-ctid", no_argument, NULL, 'k' }

Copy link
Owner

Choose a reason for hiding this comment

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

That is something that this PR is fixing, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I should have made myself clear.

I wanted to document the human errors when maintaining optstring values by hand. This bug will not be possible after we start generating the optstring values programmatically.

const char *optstring = construct_optstring(long_options);

while ((c = getopt_long(argc, argv, optstring,
long_options, &option_index)) != -1)
Expand Down
4 changes: 3 additions & 1 deletion src/bin/pgcopydb/cli_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ cli_ping_getopts(int argc, char **argv)
exit(EXIT_CODE_BAD_ARGS);
}

while ((c = getopt_long(argc, argv, "S:T:Vvdzqh",
const char *optstring = construct_optstring(long_options);

while ((c = getopt_long(argc, argv, optstring,
long_options, &option_index)) != -1)
{
switch (c)
Expand Down
4 changes: 3 additions & 1 deletion src/bin/pgcopydb/cli_restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ cli_restore_schema_getopts(int argc, char **argv)
exit(EXIT_CODE_BAD_ARGS);
}

while ((c = getopt_long(argc, argv, "S:T:D:cOXj:xF:eErRCN:Vvdzqh",
const char *optstring = construct_optstring(long_options);

while ((c = getopt_long(argc, argv, optstring,
long_options, &option_index)) != -1)
{
switch (c)
Expand Down
4 changes: 3 additions & 1 deletion src/bin/pgcopydb/cli_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ cli_stream_getopts(int argc, char **argv)
exit(EXIT_CODE_BAD_ARGS);
}

while ((c = getopt_long(argc, argv, "S:T:D:p:ws:N:o:E:rRCOIVvdzqh",
const char *optstring = construct_optstring(long_options);

while ((c = getopt_long(argc, argv, optstring,
long_options, &option_index)) != -1)
{
switch (c)
Expand Down
Loading