# ==============================================================================
"""COUNT : print the number of lines, words and chars in a set of text files"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0"
__date__    = "2022-11-12"
__usage__   = """
User input : <filename> [filename ...]
App output : number of lines, words and chars for each provided file"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def count(name:str) -> str:
  """return the number of lines, words and chars stored in file 'name'"""
  # lire le fichier dans une variable string text
  try:
    txt = read_txt(name)
  except OSError:
    return f"{name} : cannot read file"
  
  # analyser text pour compte le nombre de caractère, de mots et de lignes
  nb_char = len(txt)
  nb_word = len(txt.split())
  nb_line = len(txt.split('\n'))

  # renvoyer la chaine formatée
  # " non du fichier : lines = ___, words = ___, chars = ___
  return f"{name} : lines = {nb_line}, words = {nb_word}, chars = {nb_char}"
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' and return line/word/char counters for provided files"""
  command = parse(command)

  if type(command) is str:
    return count(command)
  else: # on a plusieurs fichiers
    result = ''
    # pour chaque fichier, appeler count et ajouter le résultat à une chaine
    # de caractère en revenant à la ligne ('\n') entre chaque appel
    for name in command:
      result += count(name) + '\n'

    return result
# ==============================================================================
if __name__ == '__main__':
  userloop(parser, "Enter <filename> [filename ...]")
# ==============================================================================
