[gn] Updating args --list to make the comment padding optional

Updating the comment to not be prefixed with three spaces for json
output and removing extraneous output.

Change-Id: Iaeee0809ce3cff050a1d3eec5f34f885677802a8
Reviewed-on: https://chromium-review.googlesource.com/1000392
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Commit-Queue: Julie Hockett <juliehockett@google.com>
Cr-Original-Commit-Position: refs/heads/master@{#548956}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 12bd7ed6ad7773920fe303d54ef1be9b4798ccb1
diff --git a/tools/gn/command_args.cc b/tools/gn/command_args.cc
index 237aa0d..9fbf67b 100644
--- a/tools/gn/command_args.cc
+++ b/tools/gn/command_args.cc
@@ -67,19 +67,22 @@
 
 // Assumes DoesLineBeginWithComment(), this strips the # character from the
 // beginning and normalizes preceding whitespace.
-std::string StripHashFromLine(const base::StringPiece& line) {
+std::string StripHashFromLine(const base::StringPiece& line, bool pad) {
   // Replace the # sign and everything before it with 3 spaces, so that a
   // normal comment that has a space after the # will be indented 4 spaces
   // (which makes our formatting come out nicely). If the comment is indented
   // from there, we want to preserve that indenting.
-  return "   " + line.substr(line.find('#') + 1).as_string();
+  if (pad)
+    return "   " + line.substr(line.find('#') + 1).as_string();
+  return line.substr(line.find('#') + 1).as_string();
 }
 
 // Tries to find the comment before the setting of the given value.
 void GetContextForValue(const Value& value,
                         std::string* location_str,
                         int* line_no,
-                        std::string* comment) {
+                        std::string* comment,
+                        bool pad_comment=true) {
   Location location = value.origin()->GetRange().begin();
   const InputFile* file = location.file();
   if (!file)
@@ -101,7 +104,7 @@
     if (!DoesLineBeginWithComment(line))
       break;
 
-    comment->insert(0, StripHashFromLine(line) + "\n");
+    comment->insert(0, StripHashFromLine(line, pad_comment) + "\n");
     line_off = previous_line_offset;
   }
 }
@@ -171,7 +174,8 @@
     if (arg.override_value.origin() && !short_only) {
       int line_no;
       std::string location, comment;
-      GetContextForValue(arg.override_value, &location, &line_no, &comment);
+      GetContextForValue(arg.override_value, &location, &line_no, &comment,
+                         /*pad_comment=*/false);
       // Omit file and line if set with --args (i.e. no file)
       if (!location.empty()) {
         override_dict.SetKey("file", base::Value(location));
@@ -188,7 +192,8 @@
   if (arg.default_value.origin() && !short_only) {
     int line_no;
     std::string location;
-    GetContextForValue(arg.default_value, &location, &line_no, &comment);
+    GetContextForValue(arg.default_value, &location, &line_no, &comment,
+                       /*pad_comment=*/false);
     // Only emit file and line if the value is overridden.
     if (arg.has_override) {
       default_dict.SetKey("file", base::Value(location));
@@ -198,11 +203,6 @@
   dict.SetKey("default", std::move(default_dict));
   if (!comment.empty() && !short_only)
     dict.SetKey("comment", base::Value(comment));
-
-  std::string s;
-  base::JSONWriter::WriteWithOptions(
-      dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &s);
-  OutputString(s);
 }
 
 int ListArgs(const std::string& build_dir) {