google-cloud-cpp icon indicating copy to clipboard operation
google-cloud-cpp copied to clipboard

Printing usage should not result in an error

Open dbolduc opened this issue 4 years ago • 5 comments

In our base Example class and several other places printing the usage results in a shell error.

This is not very important, but it seems mean to instruct users to run a command that results in an error: https://github.com/googleapis/google-cloud-cpp/blob/59d7cf6a8d3b521caf304c274fc127d636d102c3/google/cloud/spanner/samples/README.md#L55-L59

As far as I can tell we want a successful print usage call to look like this:

$ ./target --help
Usage: blah blah blah
$ echo $?
0

And for the case of examples, which have commands, we do the following to successfully print usage for a given command:

$ ./example command --help

dbolduc avatar Jun 21 '21 14:06 dbolduc

Punt once.

devjgm avatar Apr 14 '22 18:04 devjgm

Strike 2

coryan avatar Aug 25 '22 18:08 coryan

Hi @dbolduc and @devjgm , This issue seems to be a good initiation of my journey towards contribution to FOSS projects. Can you please assign this to me?

Dhruv1107 avatar Sep 09 '22 18:09 Dhruv1107

Can you please assign this to me?

Happily.


I think the first step is to follow this guide to set up your GitHub workflow: https://github.com/googleapis/google-cloud-cpp/blob/main/doc/contributor/howto-guide-forks-and-pull-requests.md

Then let's start by just building and running an example. I'd pick the pubsub samples, which uses the google::cloud::testing_util::Example class: https://github.com/googleapis/google-cloud-cpp/blob/eb5ad369a7c5fc35aba1f4678eed44b686610c5e/google/cloud/pubsub/samples/samples.cc#L2375 https://github.com/googleapis/google-cloud-cpp/blob/eb5ad369a7c5fc35aba1f4678eed44b686610c5e/google/cloud/pubsub/samples/samples.cc#L2521

We can run the example with Bazel using this command: (Warning: the initial build will be really slow)

$ bazel run //google/cloud/pubsub/samples:samples

# Output
Missing command
Usage: samples <command> [arguments]

Commands:
    create-avro-schema <project-id> <schema-id>
    ....etc....

We can then print the result of the previous command

$ echo $?

# Output
1

When that is working, we are ready to start solving the problem. We like that the program exits with a failed status when no commands are supplied. But we want to be able to pass it a --help flag and have it both

  • print the usage
  • return a 0 (success)
$ bazel run //google/cloud/pubsub/samples:samples -- --help

# Output
Usage: blah blah blah....
$ echo $?

# Current Output
1

# Desired Output
0

So the question is how should we modify the Example class to make that happen?

dbolduc avatar Sep 09 '22 19:09 dbolduc

I want to get into open source dev in c++, no idea where to start, gcs seems like a good place.

antonjfernando2021 avatar Sep 16 '22 23:09 antonjfernando2021

Seems like we do not have time to work on this. Closing for now.

coryan avatar Mar 01 '23 19:03 coryan