def ExtractArguments(toolAppContext: ToolAppContext, config: Config, exePackages: List[Package], extractArguments: str) -> Dict[Package, JsonDictType]:
    config.LogPrint("Building all executable packages to extract their command line arguments")

    filterDir = None if extractArguments == '*' else IOUtil.GetCurrentWorkingDirectory()

    res = {}  # type: Dict[Package, JsonDictType]
    for package in exePackages:
        if filterDir is None or package.AbsolutePath == filterDir:
            config.LogPrint("- Building and running {0}".format(package.Name))
            arguments = TryBuildAndRun(toolAppContext, config, package)
            if arguments is not None:
                res[package] = arguments

        # quick exit
        #return res
    return res
def __EarlyArgumentParser(
        allowStandaloneMode: bool) -> Optional[LowLevelToolConfig]:
    ### Parse the initial options this allows us to use the required debug and verbosity levels while
    ### creating the actual command line argumnets.
    debugEnabled = False
    try:
        parser = argparse.ArgumentParser(add_help=False)
        __AddDefaultOptions(parser, allowStandaloneMode)
        args, unknown = parser.parse_known_args()
        verbosityLevel = args.verbosity
        debugEnabled = True if args.debug else False
        allowDevelopmentPlugins = True if args.dev else False
        profilerEnabled = True if args.profile else False
        standaloneEnabled = False if not allowStandaloneMode else (
            True if args.standalone else False)
        currentDir = IOUtil.NormalizePath(
            args.input) if args.input is not None else None

        if currentDir is None:
            currentDir = IOUtil.GetCurrentWorkingDirectory()
        elif not IOUtil.IsDirectory(currentDir):
            raise Exception(
                "Path '{0}' specified by --input is not a directory".format(
                    currentDir))
        elif verbosityLevel > 4:
            print("Using custom path from --input '{0}'".format(currentDir))

        if args.version:
            print("V{0} Build {1}".format(CurrentVersionString,
                                          CurrentBuildString))
        return LowLevelToolConfig(verbosityLevel, debugEnabled,
                                  allowDevelopmentPlugins, profilerEnabled,
                                  standaloneEnabled, currentDir)
    except (Exception) as ex:
        print("ERROR: {0}".format(str(ex)))
        if not debugEnabled:
            return None
        raise
def __Run(appFlowFactory: AToolAppFlowFactory, strToolAppTitle: str,
          toolCommonArgConfig: ToolCommonArgConfig,
          lowLevelToolConfig: LowLevelToolConfig,
          allowStandaloneMode: bool) -> None:

    log = Log(strToolAppTitle,
              lowLevelToolConfig.VerbosityLevel,
              showAppTitleIfVerbose=True)

    generatorIds = __PrepareGeneratorPlugins(lowLevelToolConfig,
                                             toolCommonArgConfig)

    try:
        defaultPlatform = DetectBuildPlatform()
    except (Exception) as ex:
        print("ERROR: {0}".format(ex))
        if lowLevelToolConfig.DebugEnabled:
            raise
        sys.exit(1)

    ### Do the actual command line parsing
    parser = __CreateParser(toolCommonArgConfig, allowStandaloneMode)
    if toolCommonArgConfig.AddPlatformArg:
        parser.add_argument('-p',
                            '--platform',
                            default=defaultPlatform,
                            help='Select build platform: {0}'.format(
                                ", ".join(generatorIds)))
    if toolCommonArgConfig.AllowForceClaimInstallArea:
        parser.add_argument(
            '--ForceClaimInstallArea',
            action='store_true',
            help=
            'Override the security checks on the install area allowing us to use it even though its not empty. This means the existing content can be lost.'
        )

    #parser.add_argument('--NativeGen', action='store_true',  help='Force use the native build generator')

    toolConfig = None
    baseConfig = None
    try:
        basicConfig = BasicConfig(log)
        currentDir = IOUtil.GetCurrentWorkingDirectory()

        # Try to locate a project root configuration file
        projectRootConfig = GetProjectRootConfig(lowLevelToolConfig,
                                                 basicConfig, currentDir)
        toolConfigFile = projectRootConfig.ToolConfigFile

        # Get the path to the toolconfig file if necessary and load the tool config file
        toolConfigPath = __GetToolConfigPath(toolConfigFile)
        toolConfig = ToolConfig(basicConfig, toolConfigPath, projectRootConfig)
        baseConfig = BaseConfig(log, toolConfig)
    except (Exception) as ex:
        print("ERROR: {0}".format(ex))
        if lowLevelToolConfig.DebugEnabled:
            raise
        sys.exit(1)

    buildTiming = None
    try:
        defaultVSVersion = toolConfig.GetVisualStudioDefaultVersion()
        #if toolCommonArgConfig.AllowVSVersion:
        parser.add_argument(
            '--VSVersion',
            default=str(defaultVSVersion),
            help=
            'Choose a specific visual studio version (2015,2017), This project defaults to: {0}'
            .format(toolConfig.GetVisualStudioDefaultVersion()))

        userTag = appFlowFactory.CreateUserTag(baseConfig)
        appFlowFactory.AddCustomArguments(parser, toolConfig, userTag)

        args = parser.parse_args()

        #if toolCommonArgConfig.AllowVSVersion:
        PluginConfig.SetVSVersion(args.VSVersion)

        #if toolCommonArgConfig.AddPlatformArg and args.platform.lower() != PluginSharedValues.PLATFORM_ID_ALL:
        #PluginConfig.SetForceUseNativeGenerator(True)
        #PluginConfig.SetForceUseNativeGenerator(args.NativeGen)

        if toolCommonArgConfig.ProcessRemainingArgs:
            args.RemainingArgs = __ProcessRemainingArgs(args.RemainingArgs)
        if toolCommonArgConfig.SupportBuildTime and args.BuildTime:
            buildTiming = BuildTimer()

        if toolCommonArgConfig.AddBuildFiltering and args.Recipes == DefaultValue.Recipes:
            if projectRootConfig.XmlExperimental is not None:
                tmpResult = projectRootConfig.XmlExperimental.TryGetRecipesDefaultValue(
                    defaultPlatform)
                if tmpResult is not None:
                    args.Recipes = "[{0}]".format(tmpResult)

        toolAppConfig = __CreateToolAppConfig(args, defaultPlatform,
                                              toolCommonArgConfig,
                                              defaultVSVersion)
        toolAppContext = ToolAppContext(log, lowLevelToolConfig, toolAppConfig)
        toolAppFlow = appFlowFactory.Create(toolAppContext)
        toolAppFlow.ProcessFromCommandLine(args, currentDir, toolConfig,
                                           userTag)

        if buildTiming:
            PrintBuildTiming(buildTiming)
    except GroupedException as ex:
        if buildTiming:
            PrintBuildTiming(buildTiming)
        for entry in ex.ExceptionList:
            print("ERROR: {0}".format(entry))
        if lowLevelToolConfig.DebugEnabled:
            raise
        for entry in ex.ExceptionList:
            if isinstance(entry, ExitException):
                sys.exit(entry.ExitCode)
        sys.exit(1)
    except AggregateException as ex:
        if buildTiming:
            PrintBuildTiming(buildTiming)
        for entry in ex.ExceptionList:
            print("ERROR: {0}".format(entry))
        if lowLevelToolConfig.DebugEnabled:
            if len(ex.ExceptionList) > 0:
                raise ex.ExceptionList[0]
            raise
        for entry in ex.ExceptionList:
            if isinstance(entry, ExitException):
                sys.exit(entry.ExitCode)
        sys.exit(1)
    except ExitException as ex:
        sys.exit(ex.ExitCode)
    except Exception as ex:
        if buildTiming:
            PrintBuildTiming(buildTiming)
        print("ERROR: {0}".format(ex))
        if lowLevelToolConfig.DebugEnabled:
            raise
        sys.exit(1)
def __RunStandalone(appFlowFactory: AToolAppFlowFactory, strToolAppTitle: str,
                    toolCommonArgConfig: ToolCommonArgConfig,
                    lowLevelToolConfig: LowLevelToolConfig) -> None:
    log = Log(strToolAppTitle,
              lowLevelToolConfig.VerbosityLevel,
              showAppTitleIfVerbose=True)

    generatorIds = __PrepareGeneratorPlugins(lowLevelToolConfig,
                                             toolCommonArgConfig)

    ### Do the actual command line parsing
    parser = __CreateParser(toolCommonArgConfig, True)
    if toolCommonArgConfig.AddPlatformArg:
        parser.add_argument('-p',
                            '--platform',
                            required=True,
                            help='Select build platform: {0}'.format(
                                ", ".join(generatorIds)))

    buildTiming = None
    try:
        userTag = appFlowFactory.CreateStandaloneUserTag()
        appFlowFactory.AddCustomStandaloneArguments(parser, userTag)

        args = parser.parse_args()

        if toolCommonArgConfig.ProcessRemainingArgs:
            args.RemainingArgs = __ProcessRemainingArgs(args.RemainingArgs)
        if toolCommonArgConfig.SupportBuildTime and args.BuildTime:
            buildTiming = BuildTimer()

        currentDir = IOUtil.GetCurrentWorkingDirectory()

        toolAppConfig = __CreateToolAppConfig(args, args.platform,
                                              toolCommonArgConfig, 0)
        toolAppContext = ToolAppContext(log, lowLevelToolConfig, toolAppConfig)
        toolAppFlow = appFlowFactory.Create(toolAppContext)

        toolAppFlow.ProcessFromStandaloneCommandLine(args, currentDir, userTag)

        if buildTiming:
            PrintBuildTiming(buildTiming)
    except GroupedException as ex:
        if buildTiming:
            PrintBuildTiming(buildTiming)
        for entry in ex.ExceptionList:
            print("ERROR: {0}".format(entry))
        if lowLevelToolConfig.DebugEnabled:
            raise
        for entry in ex.ExceptionList:
            if isinstance(entry, ExitException):
                sys.exit(entry.ExitCode)
        sys.exit(1)
    except AggregateException as ex:
        if buildTiming:
            PrintBuildTiming(buildTiming)
        for entry in ex.ExceptionList:
            print("ERROR: {0}".format(entry))
        if lowLevelToolConfig.DebugEnabled:
            if len(ex.ExceptionList) > 0:
                raise ex.ExceptionList[0]
            raise
        for entry in ex.ExceptionList:
            if isinstance(entry, ExitException):
                sys.exit(entry.ExitCode)
        sys.exit(1)
    except ExitException as ex:
        sys.exit(ex.ExitCode)
    except Exception as ex:
        if buildTiming:
            PrintBuildTiming(buildTiming)
        print("ERROR: {0}".format(ex))
        if lowLevelToolConfig.DebugEnabled:
            raise
        sys.exit(1)