<commit_before>import cliArgs from '@pawelgalazka/cli-args'
import { isString } from 'lodash'
import path from 'path'

import { CLICommandNotFound } from './errors'
import { IAnnoations, PrintHelp, printHelp } from './help'
import { ILogger } from './logger'
import { validate } from './validate'

export interface IOptions {
  [key: string]: number | string | boolean
}

type CliCallback = (options: IOptions, ...params: string[]) => any

export function parseCommand(
  argv: string[],
  annotations: IAnnoations | string = {},
  help: PrintHelp = printHelp,
  logger: ILogger = console
) {
  return (callback: CliCallback) => {
    const { params, options } = cliArgs(argv.slice(2))
    const scriptName = path.basename(argv[1])

    if (isString(annotations)) {
      annotations = {
        description: annotations
      }
    }

    if (options.help) {
      return help(scriptName, annotations, logger)
    }

    validate(annotations, options, scriptName)

    return callback(options, ...params)
  }
}

export function parseAllCommands(
  obj: any,
  args: string[],
  logger?: ILogger,
  subtaskName?: string
) {
  const taskName = subtaskName || args[2]

  if (typeof obj[taskName] === 'function') {
    const cli = parseCommand(
      args.slice(1),
      obj[taskName].help,
      undefined,
      logger as any
    )

    cli((options, ...params) => {
      obj[taskName](options, ...params)
    })
    return obj[taskName]
  }

  const namespaces = taskName.split(':')
  const rootNamespace = namespaces.shift() || ''
  const nextSubtaskName = namespaces.join(':')

  if (obj[rootNamespace]) {
    const calledTask: any = parseAllCommands(
      obj[rootNamespace],
      args,
      logger,
      nextSubtaskName
    )
    if (calledTask) {
      return calledTask
    }
  }

  if (!subtaskName) {
    throw new CLICommandNotFound(`Command ${taskName} not found`)
  }
}
<commit_msg>Handle default command<commit_after>import cliArgs from '@pawelgalazka/cli-args'
import { isString } from 'lodash'
import path from 'path'

import { CLICommandNotFound } from './errors'
import { IAnnoations, PrintHelp, printHelp } from './help'
import { ILogger } from './logger'
import { validate } from './validate'

export interface IOptions {
  [key: string]: number | string | boolean
}

type CliCallback = (options: IOptions, ...params: string[]) => any

export function parseCommand(
  argv: string[],
  annotations: IAnnoations | string = {},
  help: PrintHelp = printHelp,
  logger: ILogger = console
) {
  return (callback: CliCallback) => {
    const { params, options } = cliArgs(argv.slice(2))
    const scriptName = path.basename(argv[1])

    if (isString(annotations)) {
      annotations = {
        description: annotations
      }
    }

    if (options.help) {
      return help(scriptName, annotations, logger)
    }

    validate(annotations, options, scriptName)

    return callback(options, ...params)
  }
}

export function parseAllCommands(
  obj: any,
  args: string[],
  logger?: ILogger,
  subtaskName?: string
) {
  const taskName = subtaskName || args[2]

  if (typeof obj[taskName] === 'function') {
    const command = parseCommand(
      args.slice(1),
      obj[taskName].help,
      undefined,
      logger as any
    )

    command((options, ...params) => {
      obj[taskName](options, ...params)
    })
    return obj[taskName]
  }

  const namespaces = taskName.split(':')
  const rootNamespace = namespaces.shift() || ''
  const nextSubtaskName = namespaces.join(':')

  if (obj[rootNamespace]) {
    const calledTask: any = parseAllCommands(
      obj[rootNamespace],
      args,
      logger,
      nextSubtaskName
    )
    if (calledTask) {
      return calledTask
    }
  }

  if (!subtaskName) {
    if (typeof obj.default === 'function') {
      const command = parseCommand(
        args,
        obj.default.help,
        undefined,
        logger as any
      )

      command((options, ...params) => {
        obj.default(options, ...params)
      })
      return obj[taskName]
    }

    throw new CLICommandNotFound(`Command ${taskName} not found`)
  }
}

{'fraction_non_alphanumeric': 0.13068873205159406, 'fraction_numerical': 0.0019469457288878072, 'mean_word_length': 2.959537572254335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '3654270', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1258, 'n_words': 323}