Repository: bazelbuild/bazel
Issue: Add -v, --version flag
Labels: type: feature request
P4
good first issue
team-OSS

Working out that `bazel version` is the right command is (for me at least) quite slow. It would be better to support the more common `-v` and `--version` flags.

```
> bazel -v
Unknown startup option: '-v'.
  For more info, run 'bazel help startup_options'.
> bazel --version
Unknown startup option: '--version'.
  For more info, run 'bazel help startup_options'.
> bazel help startup_options | grep version
> bazel help | grep version
  version             Prints version information for bazel.
> bazel version
Build label: 0.5.3
Build target: bazel-out/local-fastbuild/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Jul 28 08:34:59 2017 (1501230899)
Build timestamp: 1501230899
Build timestamp as int: 1501230899
```

### Environment info
* Operating System: Ubuntu 14.04
* Bazel version: 0.5.3

Issue comments:
`bazel info release` suffices for this, recommend it be closed.
@werkt Sure, but it doesn't solve the problem that it's non-intuitive to people who are so used to the GNU convention of -v and --version, which in my experience is by far the most common way of allowing people to query version information in other command-line tools.

So I vote to keep this open until https://github.com/bazelbuild/bazel/pull/3671 is closed.
I added a --gnu_format flag to version in 938a53d3d54ede8132109df3c492c47afc63f5a6.
However, changing the default is tricky and will require more work.
`--version` works as of 371a2e343199ee799404fd2bb8319d990a2ad572, and without starting a server!  :tada:

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc
index fd742e2392eac9..e365ad6adc38cb 100644
--- a/src/main/cpp/blaze_util.cc
+++ b/src/main/cpp/blaze_util.cc
@@ -125,7 +125,8 @@ bool SearchNullaryOption(const vector<string>& args,
 
 bool IsArg(const string& arg) {
   return blaze_util::starts_with(arg, "-") && (arg != "--help")
-      && (arg != "-help") && (arg != "-h");
+      && (arg != "-help") && (arg != "-h") && (arg != "--version")
+      && (arg != "-v");
 }
 
 void LogWait(unsigned int elapsed_seconds, unsigned int wait_seconds) {
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index 158ecb99f35723..7d6c179555733a 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -471,7 +471,7 @@ std::vector<std::string> OptionProcessor::GetBlazercAndEnvCommandArgs(
 std::vector<std::string> OptionProcessor::GetCommandArguments() const {
   assert(cmd_line_ != nullptr);
   // When the user didn't specify a command, the server expects the command
-  // arguments to be empty in order to display the help message.
+  // arguments to be empty in order to display the help or version message.
   if (cmd_line_->command.empty()) {
     return {};
   }
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
index 4325524c9c4199..91323456665f17 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
@@ -77,6 +77,9 @@ public enum LockingMode {
   private static final ImmutableSet<String> ALL_HELP_OPTIONS =
       ImmutableSet.of("--help", "-help", "-h");
 
+  private static final ImmutableSet<String> ALL_VERSION_OPTIONS =
+      ImmutableSet.of("--version", "-v");
+
   private final BlazeRuntime runtime;
   private final Object commandLock;
   private String currentClientDescription = null;
@@ -142,6 +145,11 @@ public BlazeCommandResult exec(
       commandName = "help";
     }
 
+    // Be gentle to users who want to find out Blaze version information.
+    if (ALL_VERSION_OPTIONS.contains(commandName)) {
+      commandName = "version";
+    }
+
     BlazeCommand command = runtime.getCommandMap().get(commandName);
     if (command == null) {
       outErr.printErrLn(String.format(


Test patch:
diff --git a/src/test/cpp/option_processor_test.cc b/src/test/cpp/option_processor_test.cc
index b1a59efd618134..e4530744a3d0f4 100644
--- a/src/test/cpp/option_processor_test.cc
+++ b/src/test/cpp/option_processor_test.cc
@@ -490,6 +490,14 @@ TEST_F(OptionProcessorTest, SplitCommandLineWithBlazeVersion) {
   SuccessfulSplitStartupOptionsTest(
       {"bazel", "version"},
       CommandLine("bazel", {}, "version", {}));
+
+  SuccessfulSplitStartupOptionsTest(
+      {"bazel", "-v"},
+      CommandLine("bazel", {}, "-v", {}));
+
+  SuccessfulSplitStartupOptionsTest(
+      {"bazel", "--version"},
+      CommandLine("bazel", {}, "--version", {}));
 }
 
 TEST_F(OptionProcessorTest, SplitCommandLineWithMultipleCommandArgs) {