import argparse

from mycroft import __version__, train, predict


def main():
    parser = argparse.ArgumentParser(description="Text Classifier")
    parser.add_argument("--version", action="version", version="%(prog)s " + __version__)
    parser.set_defaults(func=lambda _: parser.print_usage())

    subparsers = parser.add_subparsers(title="Commands")

    # Train subcommand
    train_parser = subparsers.add_parser("train", description="Train a model")

    data_group = train_parser.add_argument_group("data")
    data_group.add_argument("training", help="training data")
    data_group.add_argument("--limit", metavar="N", type=int,
                            help="only train on this many sample (default use all the data)")
    data_group.add_argument("--validation", metavar="PORTION", type=float,
                            help="portion of data to use for validation (default none)")
    data_group.add_argument("--text-name", metavar="NAME", default="text",
                            help="name of the text column (default 'text')")
    data_group.add_argument("--label-name", metavar="NAME", default="label",
                            help="name of the label column (default 'label')")

    model_group = train_parser.add_argument_group("model")
    model_group.add_argument("--rnn-units", metavar="N", type=int, default=128, help="RNN units (default 128)")
    model_group.add_argument("--dropout", metavar="RATE", type=float, default=0.2, help="Dropout rate (default 0.2)")

    train_group = train_parser.add_argument_group("model")
    train_group.add_argument("--epochs", metavar="N", type=int, default=10, help="training epochs (default 10)")
    train_group.add_argument("--batch-size", metavar="N", type=int, default=56, help="batch size (default 256)")
    train_group.add_argument("--model-filename", metavar="FILENAME", help="file in which to to store the model")

    train_parser.set_defaults(
        func=lambda args: train(args.training, args.text_name, args.label_name, args.limit, args.batch_size,
                                args.epochs, args.rnn_units, args.dropout, args.validation, args.model_filename))

    # Predict subcommand
    predict_parser = subparsers.add_parser("predict", description="Use a model to predict labels")
    predict_parser.add_argument("test", help="test data")
    predict_parser.add_argument("model_filename", metavar="filename", help="file containing the trained model")
    predict_parser.add_argument("--text-name", metavar="NAME", default="text",
                                help="name of the text column (default 'text')")
    predict_parser.add_argument("--limit", metavar="N", type=int,
                                help="only predict this many samples (default use all the data)")
    predict_parser.set_defaults(func=lambda args: predict(args.test, args.model_filename, args.text_name, args.limit))

    args = parser.parse_args()
    args.func(args)

@@ -8,16 +8,32 @@
 argparse
+%0Aimport textwrap
 %0A%0Afrom m
@@ -455,80 +455,416 @@
 ion=
-%22Train a model%22)%0A%0A    data_group = train_parser.add_argument_group(%22data
+textwrap.dedent(%22%22%22%0A        Train a model to predict categorical labels from English text. The training data is a comma- or tab-delimited %0A        file with column of text and a column of labels. The training and optional validation loss and accuracy are%0A        printed for each epoch.%22%22%22))%0A%0A    data_group = train_parser.add_argument_group(%22data%22, description=%22Arguments for specifying the data to train on
 %22)%0A 
@@ -1050,16 +1050,17 @@
 y sample
+s
  (defaul
@@ -1607,24 +1607,138 @@
 roup(%22model%22
+,%0A                                                  description=%22Arguments for specifying the model configuration%22
 )%0A    model_
@@ -2009,21 +2009,138 @@
 _group(%22
-model
+training%22,%0A                                                  description=%22Arguments for controlling the training procedure
 %22)%0A    t
@@ -2293,33 +2293,33 @@
 size%22, metavar=%22
-N
+M
 %22, type=int, def
@@ -2428,16 +2428,45 @@
 LENAME%22,
+%0A                            
  help=%22f
@@ -2499,16 +2499,47 @@
 he model
+ (default do not store a model)
 %22)%0A%0A    
@@ -2880,17 +2880,44 @@
 ription=
-%22
+textwrap.dedent(%22%22%22%0A        
 Use a mo
@@ -2937,17 +2937,190 @@
 t labels
-%22
+. The test data is a comma- or tab-delimited file with column of text. This%0A        prints that file, adding columns containing predicted probabilities for each category.%22%22%22)
 )%0A    pr

Improve help text