1 # -*- coding: utf-8 -*-
2 # Copyright 2014 Google Inc. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 """Contains classes related to argparse-based argument parsing."""
18 from tab_complete
import CompleterType
21 class CommandArgument(object):
22 """Argparse style argument."""
24 def __init__(self
, *args
, **kwargs
):
25 """Constructs an argparse argument with the given data.
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
33 *args: Position args to pass to argparse add_argument
34 **kwargs: Named args to pass to argparse add_argument
37 if 'completer' in kwargs
:
38 completer
= kwargs
['completer']
39 del kwargs
['completer']
42 self
.completer
= completer
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
99 def MakeFreeTextArgument():
100 """Constructs an argument that takes arbitrary text."""
101 return CommandArgument('text', completer
=CompleterType
.NO_OP
)