Home | History | Annotate | Download | only in cr
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /**
      6  * @fileoverview CommandLine class, parses out individual options from a
      7  * command line string.
      8  *
      9  * This file depends on chrome.commandLineString, which is only set if your
     10  * Web UI explicitly sets it.  The Web UI based options dialog does this from
     11  * OptionsUI::RenderViewCreated, in options_ui.cc.
     12  */
     13 
     14 cr.define('cr', function() {
     15   /**
     16    * Class to reperesent command line options passed to chrome.
     17    *
     18    * Instances of this class will have the following properties:
     19    *   executable: The name of the executable used to start chrome
     20    *
     21    *   options: An object containing the named arguments.  If the argument
     22    *   was assigned a value, such as --foo=bar, then options['--foo'] will be
     23    *   set to 'bar'.  If the argument was not assigned a value, such as
     24    *   --enable-foo, then options['--enable-foo'] will be set to true.
     25    *
     26    *   looseArguments: An array of arguments that were not associated with
     27    *   argument names.
     28    *
     29    * Note that the Chromium code that computes the command line string
     30    * has a bug that strips quotes from command lines, so you can't really
     31    * trust looseArguments or any argument that might contain spaces until
     32    * http://code.google.com/p/chromium/issues/detail?id=56684 is fixed.
     33    *
     34    * @param {string} commandLineString The command line string to parse.
     35    */
     36   function CommandLine(commandLineString) {
     37     this.commandLineString_ = commandLineString;
     38     this.parseOptions_(commandLineString.split(/\s+/));
     39   }
     40 
     41   /**
     42    * Return the command line as a single string.
     43    */
     44   CommandLine.prototype.toString = function() {
     45     return this.commandLineString_;
     46   };
     47 
     48   /**
     49    * Parse the array of command line options into this.executable, this.options,
     50    * and this.looseArguments.
     51    *
     52    * @param {Array} ary The list of command line arguments.  The first argument
     53    *     must be the executable name.  Named command line arguments must start
     54    *     with two dashes, and may optionally be assigned a value as in
     55    *     --argument-name=value.
     56    */
     57   CommandLine.prototype.parseOptions_ = function(ary) {
     58     this.executable = ary.shift();
     59     this.options = {};
     60     this.looseArguments = [];
     61 
     62     for (var i = 0; i < ary.length; i++) {
     63       var arg = ary[i];
     64 
     65       if (arg.substr(0, 2) == '--') {
     66         var pos = arg.indexOf('=');
     67         if (pos > 0) {
     68           // Argument has a value: --argument-name=value
     69           this.options[arg.substr(0, pos)] = arg.substr(pos + 1);
     70         } else {
     71           // Argument is a flag: --some-flag
     72           this.options[arg] = true;
     73         }
     74       } else {
     75         // Argument doesn't start with '--'.
     76         this.looseArguments.push(arg);
     77       }
     78     }
     79   };
     80 
     81   var commandLine = null;
     82   if (chrome && chrome.commandLineString) {
     83     commandLine = new CommandLine(chrome.commandLineString);
     84   } else {
     85     console.warn('chrome.commandLineString is not present.  Not initializing ' +
     86                  'cr.commandLine');
     87   }
     88 
     89   return {
     90     CommandLine: CommandLine,
     91     commandLine: commandLine
     92   };
     93 });
     94