Fix up the command line, add flags.

Change-Id: I420bd0212fc7541668bd095b88295564d3d11f6c
diff --git a/tools/aapt2/Flag.cpp b/tools/aapt2/Flag.cpp
index a563bfc..3b2ff51 100644
--- a/tools/aapt2/Flag.cpp
+++ b/tools/aapt2/Flag.cpp
@@ -16,6 +16,7 @@
     std::function<void(const StringPiece&)> action;
     bool required;
     bool* flagResult;
+    bool flagValueWhenSet;
     bool parsed;
 };
 
@@ -25,18 +26,19 @@
 void optionalFlag(const StringPiece& name, const StringPiece& description,
                   std::function<void(const StringPiece&)> action) {
     sFlags.push_back(
-            Flag{ name.toString(), description.toString(), action, false, nullptr, false });
+            Flag{ name.toString(), description.toString(), action, false, nullptr, false, false });
 }
 
 void requiredFlag(const StringPiece& name, const StringPiece& description,
                   std::function<void(const StringPiece&)> action) {
     sFlags.push_back(
-            Flag{ name.toString(), description.toString(), action, true, nullptr, false });
+            Flag{ name.toString(), description.toString(), action, true, nullptr, false, false });
 }
 
-void optionalSwitch(const StringPiece& name, const StringPiece& description, bool* result) {
-    sFlags.push_back(
-            Flag{ name.toString(), description.toString(), {}, false, result, false });
+void optionalSwitch(const StringPiece& name, const StringPiece& description, bool resultWhenSet,
+                    bool* result) {
+    sFlags.push_back(Flag{
+            name.toString(), description.toString(), {}, false, result, resultWhenSet, false });
 }
 
 void usageAndDie(const StringPiece& command) {
@@ -73,7 +75,7 @@
                 match = true;
                 flag.parsed = true;
                 if (flag.flagResult) {
-                    *flag.flagResult = true;
+                    *flag.flagResult = flag.flagValueWhenSet;
                 } else {
                     i++;
                     if (i >= argc) {
diff --git a/tools/aapt2/Flag.h b/tools/aapt2/Flag.h
index 4cadfc4..4745c35 100644
--- a/tools/aapt2/Flag.h
+++ b/tools/aapt2/Flag.h
@@ -16,7 +16,8 @@
 void optionalFlag(const StringPiece& name, const StringPiece& description,
                   std::function<void(const StringPiece&)> action);
 
-void optionalSwitch(const StringPiece& name, const StringPiece& description, bool* result);
+void optionalSwitch(const StringPiece& name, const StringPiece& description, bool resultWhenSet,
+                    bool* result);
 
 void usageAndDie(const StringPiece& command);
 
diff --git a/tools/aapt2/Main.cpp b/tools/aapt2/Main.cpp
index b3e2768..03b9ba4 100644
--- a/tools/aapt2/Main.cpp
+++ b/tools/aapt2/Main.cpp
@@ -49,6 +49,8 @@
 #include <unordered_set>
 #include <utils/Errors.h>
 
+constexpr const char* kAaptVersionStr = "2.0-alpha";
+
 using namespace aapt;
 
 void printTable(const ResourceTable& table) {
@@ -318,8 +320,14 @@
     // Whether to output verbose details about
     // compilation.
     bool verbose = false;
+
+    // Whether or not to auto-version styles or layouts
+    // referencing attributes defined in a newer SDK
+    // level than the style or layout is defined for.
+    bool versionStylesAndLayouts = true;
 };
 
+
 bool compileXml(const AaptOptions& options, const std::shared_ptr<ResourceTable>& table,
                 const CompileItem& item, std::queue<CompileItem>* outQueue, ZipFile* outApk) {
     std::ifstream in(item.source.path, std::ifstream::binary);
@@ -333,10 +341,12 @@
     // No resolver, since we are not compiling attributes here.
     XmlFlattener flattener(table, {});
 
-    // We strip attributes that do not belong in this version of the resource.
-    // Non-version qualified resources have an implicit version 1 requirement.
     XmlFlattener::Options xmlOptions;
-    xmlOptions.maxSdkAttribute = item.config.sdkVersion ? item.config.sdkVersion : 1;
+    if (options.versionStylesAndLayouts) {
+        // We strip attributes that do not belong in this version of the resource.
+        // Non-version qualified resources have an implicit version 1 requirement.
+        xmlOptions.maxSdkAttribute = item.config.sdkVersion ? item.config.sdkVersion : 1;
+    }
 
     std::shared_ptr<BindingXmlPullParser> binding;
     std::shared_ptr<XmlPullParser> parser = std::make_shared<SourceXmlPullParser>(in);
@@ -526,7 +536,10 @@
 
     AaptOptions options;
 
-    if (command == "link") {
+    if (command == "--version" || command == "version") {
+        std::cout << kAaptVersionStr << std::endl;
+        exit(0);
+    } else if (command == "link") {
         options.phase = AaptOptions::Phase::Link;
     } else if (command == "compile") {
         options.phase = AaptOptions::Phase::Compile;
@@ -544,6 +557,8 @@
                 [&options](const StringPiece& arg) {
                     options.bindingOutput = Source{ arg.toString() };
                 });
+        flag::optionalSwitch("--no-version", "Disables automatic style and layout versioning",
+                             false, &options.versionStylesAndLayouts);
 
     } else if (options.phase == AaptOptions::Phase::Link) {
         flag::requiredFlag("--manifest", "AndroidManifest.xml of your app",
@@ -568,8 +583,8 @@
     });
 
     bool help = false;
-    flag::optionalSwitch("-v", "enables verbose logging", &options.verbose);
-    flag::optionalSwitch("-h", "displays this help menu", &help);
+    flag::optionalSwitch("-v", "enables verbose logging", true, &options.verbose);
+    flag::optionalSwitch("-h", "displays this help menu", true, &help);
 
     // Build the command string for output (eg. "aapt2 compile").
     std::string fullCommand = "aapt2";
@@ -896,7 +911,9 @@
     }
 
     // Version all styles referencing attributes outside of their specified SDK version.
-    versionStylesForCompat(table);
+    if (options.versionStylesAndLayouts) {
+        versionStylesForCompat(table);
+    }
 
     // Open the output APK file for writing.
     ZipFile outApk;