<commit_before>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
 * The main entrypoint for the pub command line application.
 */
library pub;

import '../../pkg/args/lib/args.dart';
import 'dart:io';
import 'dart:math';
import 'io.dart';
import 'command_help.dart';
import 'command_install.dart';
import 'command_lish.dart';
import 'command_update.dart';
import 'command_version.dart';
import 'entrypoint.dart';
import 'exit_codes.dart' as exit_codes;
import 'git_source.dart';
import 'hosted_source.dart';
import 'package.dart';
import 'pubspec.dart';
import 'sdk_source.dart';
import 'source.dart';
import 'source_registry.dart';
import 'system_cache.dart';
import 'utils.dart';
import 'version.dart';

Version get pubVersion => new Version(0, 0, 0);

/**
 * The commands that Pub understands.
 */
Map<String, PubCommand> get pubCommands {
  var commands = {
    'help': new HelpCommand(),
    'install': new InstallCommand(),
    'publish': new LishCommand(),
    'update': new UpdateCommand(),
    'version': new VersionCommand()
  };
  for (var command in commands.values) {
    for (var alias in command.aliases) {
      commands[alias] = command;
    }
  }
  return commands;
}

/**
 * The parser for arguments that are global to Pub rather than specific to a
 * single command.
 */
ArgParser get pubArgParser {
  var parser = new ArgParser();
  parser.addFlag('help', abbr: 'h', negatable: false,
    help: 'Prints this usage information');
  parser.addFlag('version', negatable: false,
    help: 'Prints the version of Pub');
  parser.addFlag('trace', help: 'Prints a stack trace when an error occurs');
  return parser;
}

main() {
  var globalOptions;
  try {
    globalOptions = pubArgParser.parse(new Options().arguments);
  } on FormatException catch (e) {
    printError(e.message);
    printError('Run "pub help" to see available options.');
    exit(exit_codes.USAGE);
  }

  if (globalOptions['version']) {
    printVersion();
    return;
  }

  if (globalOptions['help'] || globalOptions.rest.isEmpty) {
    printUsage();
    return;
  }

  // TODO(nweiz): Have a fallback for this this out automatically once 1145 is
  // fixed.
  var sdkDir = Platform.environment['DART_SDK'];
  var cacheDir;
  if (Platform.environment.containsKey('PUB_CACHE')) {
    cacheDir = Platform.environment['PUB_CACHE'];
  } else if (Platform.operatingSystem == 'windows') {
    var appData = Platform.environment['APPDATA'];
    cacheDir = join(appData, 'Pub', 'Cache');
  } else {
    cacheDir = '${Platform.environment['HOME']}/.pub-cache';
  }

  var cache = new SystemCache(cacheDir);
  cache.register(new SdkSource(sdkDir));
  cache.register(new GitSource());
  cache.register(new HostedSource());
  cache.sources.setDefault('hosted');

  // Select the command.
  var command = pubCommands[globalOptions.rest[0]];
  if (command == null) {
    printError('Could not find a command named "${globalOptions.rest[0]}".');
    printError('Run "pub help" to see available commands.');
    exit(exit_codes.USAGE);
    return;
  }

  var commandArgs =
      globalOptions.rest.getRange(1, globalOptions.rest.length - 1);
  command.run(cache, globalOptions, commandArgs);
}

/** Displays usage information for the app. */
void printUsage([String description = 'Pub is a package manager for Dart.']) {
  print(description);
  print('');
  print('Usage: pub command [arguments]');
  print('');
  print('Global options:');
  print(pubArgParser.getUsage());
  print('');

  // Show the commands sorted.
  print('Available commands:');

  // TODO(rnystrom): A sorted map would be nice.
  int length = 0;
  var names = <String>[];
  for (var command in pubCommands.keys) {
    length = max(length, command.length);
    names.add(command);
  }

  names.sort((a, b) => a.compareTo(b));

  for (var name in names) {
    print('  ${padRight(name, length)}   ${pubCommands[name].description}');
  }

  print('');
  print('Use "pub help [command]" for more information about a command.');
}

void printVersion() {
  print('Pub $pubVersion');<commit_msg>Hide aliases when printing 'pub --help'.<commit_after>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
 * The main entrypoint for the pub command line application.
 */
library pub;

import '../../pkg/args/lib/args.dart';
import 'dart:io';
import 'dart:math';
import 'io.dart';
import 'command_help.dart';
import 'command_install.dart';
import 'command_lish.dart';
import 'command_update.dart';
import 'command_version.dart';
import 'entrypoint.dart';
import 'exit_codes.dart' as exit_codes;
import 'git_source.dart';
import 'hosted_source.dart';
import 'package.dart';
import 'pubspec.dart';
import 'sdk_source.dart';
import 'source.dart';
import 'source_registry.dart';
import 'system_cache.dart';
import 'utils.dart';
import 'version.dart';

Version get pubVersion => new Version(0, 0, 0);

/**
 * The commands that Pub understands.
 */
Map<String, PubCommand> get pubCommands {
  var commands = {
    'help': new HelpCommand(),
    'install': new InstallCommand(),
    'publish': new LishCommand(),
    'update': new UpdateCommand(),
    'version': new VersionCommand()
  };
  for (var command in commands.values) {
    for (var alias in command.aliases) {
      commands[alias] = command;
    }
  }
  return commands;
}

/**
 * The parser for arguments that are global to Pub rather than specific to a
 * single command.
 */
ArgParser get pubArgParser {
  var parser = new ArgParser();
  parser.addFlag('help', abbr: 'h', negatable: false,
    help: 'Prints this usage information');
  parser.addFlag('version', negatable: false,
    help: 'Prints the version of Pub');
  parser.addFlag('trace', help: 'Prints a stack trace when an error occurs');
  return parser;
}

main() {
  var globalOptions;
  try {
    globalOptions = pubArgParser.parse(new Options().arguments);
  } on FormatException catch (e) {
    printError(e.message);
    printError('Run "pub help" to see available options.');
    exit(exit_codes.USAGE);
  }

  if (globalOptions['version']) {
    printVersion();
    return;
  }

  if (globalOptions['help'] || globalOptions.rest.isEmpty) {
    printUsage();
    return;
  }

  // TODO(nweiz): Have a fallback for this this out automatically once 1145 is
  // fixed.
  var sdkDir = Platform.environment['DART_SDK'];
  var cacheDir;
  if (Platform.environment.containsKey('PUB_CACHE')) {
    cacheDir = Platform.environment['PUB_CACHE'];
  } else if (Platform.operatingSystem == 'windows') {
    var appData = Platform.environment['APPDATA'];
    cacheDir = join(appData, 'Pub', 'Cache');
  } else {
    cacheDir = '${Platform.environment['HOME']}/.pub-cache';
  }

  var cache = new SystemCache(cacheDir);
  cache.register(new SdkSource(sdkDir));
  cache.register(new GitSource());
  cache.register(new HostedSource());
  cache.sources.setDefault('hosted');

  // Select the command.
  var command = pubCommands[globalOptions.rest[0]];
  if (command == null) {
    printError('Could not find a command named "${globalOptions.rest[0]}".');
    printError('Run "pub help" to see available commands.');
    exit(exit_codes.USAGE);
    return;
  }

  var commandArgs =
      globalOptions.rest.getRange(1, globalOptions.rest.length - 1);
  command.run(cache, globalOptions, commandArgs);
}

/** Displays usage information for the app. */
void printUsage([String description = 'Pub is a package manager for Dart.']) {
  print(description);
  print('');
  print('Usage: pub command [arguments]');
  print('');
  print('Global options:');
  print(pubArgParser.getUsage());
  print('');

  // Show the commands sorted.
  print('Available commands:');

  // TODO(rnystrom): A sorted map would be nice.
  int length = 0;
  var names = <String>[];
  for (var command in pubCommands.keys) {
    // Hide aliases.
    if (pubCommands[command].aliases.indexOf(command) >= 0) continue;
    length = max(length, command.length);
    names.add(command);
  }

  names.sort((a, b) => a.compareTo(b));

  for (var name in names) {
    print('  ${padRight(name, length)}   ${pubCommands[name].description}');
  }

  print('');
  print('Use "pub help [command]" for more information about a command.');
}

void printVersion() {
  print('Pub $pubVersion');
{'fraction_non_alphanumeric': 0.14615564932020628, 'fraction_numerical': 0.003867791842475387, 'mean_word_length': 3.9959016393442623, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4963117', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2644, 'n_words': 778}