Skip to content

Instantly share code, notes, and snippets.

@durgaswaroop
Created November 8, 2020 15:11
Show Gist options
  • Save durgaswaroop/9c140a97d18be453cef5225dbc1f2444 to your computer and use it in GitHub Desktop.
Save durgaswaroop/9c140a97d18be453cef5225dbc1f2444 to your computer and use it in GitHub Desktop.
Making a git like command line application with argpase in python

This is just to show how to use argparse to create a git like command line structure. It does not have any actual functionality right now.

Output of --help is shown below in this file. The actual code with argparse will be in the other file of this gist.

 % python git_clone_cmd.py clone --help
usage: git_clone_cmd.py clone [-h] [--template <template_directory>] [--local]
                              <repository> [<directory>]

positional arguments:
  <repository>          The (possibly remote) repository to clone from
  <directory>           The name of a new directory to clone into

optional arguments:
  -h, --help            show this help message and exit
  --template <template_directory>
                        Specify the directory from which templates will be
                        used
  --local, -l           When the repository to clone from is on a local
                        machine, this flag bypasses the normal "Git aware"
                        transport mechanism and clones the repository by
                        making a copy of HEAD and everything under objects and
                        refs directories. The files under .git/objects/
                        directory are hardlinked to save space when possible.
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
# clone
parse_clone = subparsers.add_parser('clone', help='Clone a repository into a new directory')
parse_clone.add_argument('--template', metavar='<template_directory>', help='Specify the directory from which templates will be used')
parse_clone.add_argument('--local', '-l', action='store_true', help='''When the repository to clone from is on a local machine, this flag bypasses the normal "Git aware" transport mechanism and clones the repository by making a copy of HEAD and everything under objects and refs directories. The files under .git/objects/ directory are hardlinked to save space when possible.''')
parse_clone.add_argument('repository', metavar='<repository>', help='The (possibly remote) repository to clone from')
parse_clone.add_argument('directory', nargs='?', metavar='<directory>', help='The name of a new directory to clone into')
args = parser.parse_args()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment