Home | History | Annotate | Download | only in gslib
      1 # -*- coding: utf-8 -*-
      2 # Copyright 2014 Google Inc. All Rights Reserved.
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 
     16 """Contains classes related to argparse-based argument parsing."""
     17 
     18 from tab_complete import CompleterType
     19 
     20 
     21 class CommandArgument(object):
     22   """Argparse style argument."""
     23 
     24   def __init__(self, *args, **kwargs):
     25     """Constructs an argparse argument with the given data.
     26 
     27     See add_argument in argparse for description of the options.
     28     The only deviation from the argparse arguments is the 'completer' parameter.
     29     If 'completer' is present, it's used as the argcomplete completer for the
     30     argument.
     31 
     32     Args:
     33       *args: Position args to pass to argparse add_argument
     34       **kwargs: Named args to pass to argparse add_argument
     35     """
     36     completer = None
     37     if 'completer' in kwargs:
     38       completer = kwargs['completer']
     39       del kwargs['completer']
     40     self.args = args
     41     self.kwargs = kwargs
     42     self.completer = completer
     43 
     44   @staticmethod
     45   def MakeZeroOrMoreCloudURLsArgument():
     46     """Constructs an argument that takes 0 or more Cloud URLs as parameters."""
     47     return CommandArgument(
     48         'file', nargs='*', completer=CompleterType.CLOUD_OBJECT)
     49 
     50   @staticmethod
     51   def MakeZeroOrMoreCloudBucketURLsArgument():
     52     """Constructs an argument that takes 0+ Cloud bucket URLs as parameters."""
     53     return CommandArgument(
     54         'file', nargs='*', completer=CompleterType.CLOUD_BUCKET)
     55 
     56   @staticmethod
     57   def MakeNCloudBucketURLsArgument(n):
     58     """Constructs an argument that takes N Cloud bucket URLs as parameters."""
     59     return CommandArgument(
     60         'file', nargs=n, completer=CompleterType.CLOUD_BUCKET)
     61 
     62   @staticmethod
     63   def MakeNCloudURLsArgument(n):
     64     """Constructs an argument that takes N Cloud URLs as parameters."""
     65     return CommandArgument(
     66         'file', nargs=n, completer=CompleterType.CLOUD_OBJECT)
     67 
     68   @staticmethod
     69   def MakeZeroOrMoreCloudOrFileURLsArgument():
     70     """Constructs an argument that takes 0 or more Cloud or File URLs."""
     71     return CommandArgument(
     72         'file', nargs='*', completer=CompleterType.CLOUD_OR_LOCAL_OBJECT)
     73 
     74   @staticmethod
     75   def MakeNCloudOrFileURLsArgument(n):
     76     """Constructs an argument that takes N Cloud or File URLs as parameters."""
     77     return CommandArgument(
     78         'file', nargs=n, completer=CompleterType.CLOUD_OR_LOCAL_OBJECT)
     79 
     80   @staticmethod
     81   def MakeZeroOrMoreFileURLsArgument():
     82     """Constructs an argument that takes 0 or more File URLs as parameters."""
     83     return CommandArgument(
     84         'file', nargs='*', completer=CompleterType.LOCAL_OBJECT)
     85 
     86   @staticmethod
     87   def MakeNFileURLsArgument(n):
     88     """Constructs an argument that takes N File URLs as parameters."""
     89     return CommandArgument(
     90         'file', nargs=n, completer=CompleterType.LOCAL_OBJECT)
     91 
     92   @staticmethod
     93   def MakeFileURLOrCannedACLArgument():
     94     """Constructs an argument that takes a File URL or a canned ACL."""
     95     return CommandArgument(
     96         'file', nargs=1, completer=CompleterType.LOCAL_OBJECT_OR_CANNED_ACL)
     97 
     98   @staticmethod
     99   def MakeFreeTextArgument():
    100     """Constructs an argument that takes arbitrary text."""
    101     return CommandArgument('text', completer=CompleterType.NO_OP)
    102