Skip to content

differentiating required and optional arguments in the help text for … #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions samples/vmc/helpers/sample_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
* *******************************************************
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
* SPDX-License-Identifier: MIT
* *******************************************************
*
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
"""

__author__ = 'VMware, Inc.'
__copyright__ = 'Copyright 2019 VMware, Inc. All rights reserved.'

import argparse

"""
Builds a standard argument parser with required and optional argument groups

Most of the VMC samples require these three standard required arguments.
If any of these arguments are not required, then build your own parser

--refresh_token
--org_id
--sddc_id

"""
parser = argparse.ArgumentParser()

required_args = parser.add_argument_group(
'required arguments')
optional_args = parser.add_argument_group(
'optional arguments')

required_args.add_argument(
'--refresh_token',
required=True,
help='Refresh token obtained from CSP')
required_args.add_argument(
'--org_id',
required=True,
help='Orgization ID')
required_args.add_argument(
'--sddc_id',
required=True,
help='SDDC ID')
17 changes: 1 addition & 16 deletions samples/vmc/networks_nsxt/cgw_firewall_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
__author__ = 'VMware, Inc.'
__vcenter_version__ = '6.8.0+'

import argparse
import requests
from samples.vmc.helpers.sample_cli import parser
from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc
from com.vmware.nsx_policy.model_client import IPAddressExpression
from com.vmware.nsx_policy.model_client import Group
Expand All @@ -36,21 +36,6 @@ class NSXPolicyCGWFirewall(object):
"""

def __init__(self):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

args = parser.parse_args()

self.nsx_client = create_nsx_policy_client_for_vmc(
Expand Down
17 changes: 1 addition & 16 deletions samples/vmc/networks_nsxt/dfw_firewall_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
__author__ = 'VMware, Inc.'
__vcenter_version__ = '6.8.0+'

import argparse
import requests
from samples.vmc.helpers.sample_cli import parser
from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc
from com.vmware.nsx_policy.model_client import IPAddressExpression
from com.vmware.nsx_policy.model_client import Group
Expand All @@ -38,21 +38,6 @@ class NSXPolicyDFWFirewall(object):
"""

def __init__(self):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

args = parser.parse_args()

self.nsx_client = create_nsx_policy_client_for_vmc(
Expand Down
17 changes: 1 addition & 16 deletions samples/vmc/networks_nsxt/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@

__author__ = 'VMware, Inc.'

import argparse
import pprint

from samples.vmc.helpers.sample_cli import parser
from com.vmware.nsx_policy_client_for_vmc import (
create_nsx_policy_client_for_vmc)

Expand All @@ -33,20 +32,6 @@ class AuthExample(object):
"""

def __init__(self):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-r', '--refresh-token',
required=True,
help='VMware Cloud API refresh token')

parser.add_argument('-o', '--org-id',
required=True,
help='Organization identifier.')

parser.add_argument('-s', '--sddc-id',
required=True,
help='Sddc Identifier.')

args = parser.parse_args()

self.org_id = args.org_id
Expand Down
21 changes: 3 additions & 18 deletions samples/vmc/networks_nsxt/l3_vpn_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

__author__ = 'VMware, Inc.'

import argparse
from samples.vmc.helpers.sample_cli import parser, required_args
from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc
from vmware.vapi.bindings.struct import PrettyPrinter as NsxPrettyPrinter
from com.vmware.nsx_policy.model_client import ApiError
Expand All @@ -39,26 +39,11 @@ class NSXPolicyL3VPN(object):
"""

def __init__(self):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

parser.add_argument('--remote_endpoint_public_ip',
required_args.add_argument('--remote_endpoint_public_ip',
required=True,
help='L3 VPN Remote end point\'s public ip')

parser.add_argument('--passphrase',
required_args.add_argument('--passphrase',
required=True,
help='Passphrase used for VPN')

Expand Down
18 changes: 2 additions & 16 deletions samples/vmc/networks_nsxt/nat_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
__author__ = 'VMware, Inc.'
__vcenter_version__ = '6.8.1+'

import argparse
import requests

from samples.vmc.helpers.sample_cli import parser
from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc
from com.vmware.nsx_vmc_app_client_for_vmc import create_nsx_vmc_app_client_for_vmc
from com.vmware.nsx_vmc_app.model_client import PublicIp
Expand All @@ -36,21 +37,6 @@ class NSXPolicyNAT(object):
"""

def __init__(self):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

args = parser.parse_args()

self.nsx_client = create_nsx_policy_client_for_vmc(
Expand Down
26 changes: 6 additions & 20 deletions samples/vmc/networks_nsxt/security_group_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
__author__ = 'VMware, Inc'
__vcenter_version__ = 'VMware Cloud on AWS'

import argparse
import random

import requests

from samples.vmc.helpers.sample_cli import parser, required_args, optional_args
from com.vmware.nsx_policy.infra_client import Domains
from com.vmware.nsx_policy.model_client import (Expression, Group,
IPAddressExpression)
Expand All @@ -35,34 +36,19 @@
Sample Prerequisites:
- SDDC deployed in VMware Cloud on AWS
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

parser.add_argument('--gateway_type',
optional_args.add_argument('--gateway_type',
default='mgw',
help='Gateway type. Either mgw or cgw')

parser.add_argument('--name',
required_args.add_argument('--name',
required=True,
help='Name of the security group to be created')

parser.add_argument('--ip_address',
optional_args.add_argument('--ip_address',
default='172.31.0.0/24',
help='IP address for the expression')

parser.add_argument('--group_id',
optional_args.add_argument('--group_id',
help='ID of the group. A random ID will be used by default')

args = parser.parse_args()
Expand Down
21 changes: 3 additions & 18 deletions samples/vmc/networks_nsxt/security_group_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
__author__ = 'VMware, Inc'
__vcenter_version__ = 'VMware Cloud on AWS'

import argparse
import random

import requests
from samples.vmc.helpers.sample_cli import parser, optional_args
from com.vmware.nsx_policy.infra_client import Domains
from com.vmware.nsx_policy.model_client import (Expression, Group,
IPAddressExpression)
Expand All @@ -39,26 +39,11 @@
- SDDC deployed in VMware Cloud on AWS
- A NSX-T security group
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

parser.add_argument('--gateway_type',
optional_args.add_argument('--gateway_type',
default='mgw',
help='Gateway type. Either mgw or cgw')

parser.add_argument('--group_id',
optional_args.add_argument('--group_id',
help='ID of the group to be deleted')

args = parser.parse_args()
Expand Down
19 changes: 2 additions & 17 deletions samples/vmc/networks_nsxt/security_group_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
__vcenter_version__ = 'VMware Cloud on AWS'

import requests
import argparse

from samples.vmc.helpers.sample_cli import parser, optional_args
from com.vmware.nsx_policy.infra_client import Domains
from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc
from vmware.vapi.bindings.struct import PrettyPrinter
Expand All @@ -34,22 +34,7 @@
Sample Prerequisites:
- SDDC deployed in VMware Cloud on AWS
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

parser.add_argument('--gateway_type',
optional_args.add_argument('--gateway_type',
default='mgw',
help='Gateway type. Either mgw or cgw')

Expand Down
23 changes: 4 additions & 19 deletions samples/vmc/networks_nsxt/security_group_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
__author__ = 'VMware, Inc'
__vcenter_version__ = 'VMware Cloud on AWS'

import argparse
import random

import requests
from samples.vmc.helpers.sample_cli import parser, required_args, optional_args
from com.vmware.nsx_policy.infra_client import Domains
from com.vmware.nsx_policy.model_client import (Expression, Group,
IPAddressExpression)
Expand All @@ -40,29 +40,14 @@
- SDDC deployed in VMware Cloud on AWS
- A NSX-T security group
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--refresh_token',
required=True,
help='Refresh token obtained from CSP')

parser.add_argument('--org_id',
required=True,
help='Orgization ID')

parser.add_argument('--sddc_id',
required=True,
help='Sddc ID')

parser.add_argument('--gateway_type',
optional_args.add_argument('--gateway_type',
default='mgw',
help='Gateway type. Either mgw or cgw')

parser.add_argument('--group_id',
optional_args.add_argument('--group_id',
help='ID of the group to be updated')

parser.add_argument('--name',
required_args.add_argument('--name',
required=True,
help='New name of the security group to be updated')

Expand Down
Loading