0

I'm working with Click-7.1.2 in order to import data to MongoDB by Pymongo. Because I'm new to Click, so that I put all options in one function main() .After that, I will get values of those options received over the command-line and pass to other functions.

def import_data(processed_data):
    # do something with the processed_data


def filter_time(processed_data, timeline):
    # filter time and return data


@click.command()
@click.option('--file', 'filepath', prompt="File pạth please", help='Choose your file path.', type=click.Path(exists=True, path_type=type(json)))
@click.option('--from', 'timeline', help='Choose your datetime.', type=click.DateTime(formats=["%d/%m/%Y"]))
@click.option('--silent', 'silent_mode', help='Disable log.', is_flag=True)
def main(filepath, timeline, silent_mode):
    if silent_mode:
        logger.disabled = True
    result = []
    logger.info("Read file: %s", filepath.decode('utf-8'))
    with open(filepath) as f:
        # return processed_data

    if not timeline:
        import_data(processed_data)
    else:
        import_data(filter_time(processed_data, timeline))

As you can see, the main() function has only three options now. What can I do when it becomes bigger and has more options or arguments? Of course, not every option is to add a decorator to the beginning of the function. I don't think that it is efficient to split an option with each function, which can lead to difficulty calling variables once they are local variables. Has anyone ever met this situation? Please help me! I want to write code that is scalable and clean.

Thank you!

0 Answers0